我的laravel cron作业无法自动运行。我对其进行了测试,并且似乎可以正常工作,当我使用php artisan checkCommissions:check_commissions时,它可以完美地运行我的任务。所以我认为问题出在我用cpanel编写的命令中,所以有什么帮助吗?
app \ Console \ Commands \ CheckCommissions.php:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use App\Models\Setting;
use App\Models\Commission;
class checkCommissions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'checkCommissions:check_commissions';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check users counters and give them commissions';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$maxout = Setting::where('name', 'maxout')->first();
if (!is_null($maxout)) {
$maxout = $maxout->value;
}
if (is_null($maxout) || !is_numeric($maxout) || $maxout < 0) {
$maxout = 0;
}
$commission = Setting::where('name', 'commission')->first();
if (!is_null($commission)) {
$commission = $commission->value;
}
if (is_null($commission) || !is_numeric($commission) || $commission < 0) {
$commission = 0;
}
$users = User::all();
foreach ($users as $user) {
if ($user->counter_right >= $maxout && $user->counter_left >= $maxout) {
$num = $maxout;
$update_counters = User::where('id', $user->id)->update(['counter_left' => 0, 'counter_right' => 0]);
} else {
if ($user->counter_right >= $user->counter_left) {
$num = $user->counter_left;
} else {
$num = $user->counter_right;
}
$update_counters = User::where('id', $user->id)->update(['counter_left' => $user->counter_left - $num, 'counter_right' => $user->counter_right - $num]);
}
if($num > 0) {
$money = $num * ($commission / 2);
$update_user = User::where('id', $user->id)->update(['money' => $user->money + $money]);
Commission::insert(['user_id' => $user->id, 'money' => $money]);
}
}
}
}
app \ Console \ kernel.php:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
'\App\Console\Commands\checkCommissions',
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('checkCommissions:check_commissions')
->everyMinute(); //
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
以及Cpanel中的命令:
分钟小时日月份工作日命令操作
0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1
答案 0 :(得分:1)
首先,欢迎使用StackOverflow。是的,您的命令是错误的...您想在应用程序控制台内核中运行计划的命令,而不是计划命令的执行。请阅读有关Task scheduling(可能还有cron wiki)的Laravel官方文档。
您只是告诉CPanel crontab每天在午夜运行命令checkCommissions:check_commissions
,但是在调度程序中,您希望每分钟运行该命令。
首先,您必须从以下位置更新您的cronjob
0 0 * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan checkCommissions:check_commissions >> /dev/null 2>&1
到
* * * * * /usr/local/bin/php /home/gridtumg/public_html/tree/local/artisan schedule:run >> /dev/null 2>&1
为了启动调度程序。然后laravel将处理其余的事情。