带有 Cron 作业的 Laravel 调度器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37317967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Scheduler with Cron Job
提问by yudijohn
I'm using larave 5.1 with php5, i try to create my cron job to remove unpaid invoice in time i want, but i testing it to print an userlog to help me know that the job is working.
我正在使用带有 php5 的 larave 5.1,我尝试创建我的 cron 作业以及时删除未付发票,但我测试它以打印用户日志以帮助我知道该作业正在运行。
This is my app/Console/Kernel.php
这是我的应用程序/控制台/Kernel.php
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\RemoveUnpaidInvoice::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('removeUnpaidInvoice')->everyMinute();
// $schedule->command('inspire')->hourly();
}
And this is my RemoveUnpaidInvoice class :
这是我的 RemoveUnpaidInvoice 类:
public function handle()
{
UserLog::create([
'user_id' => '3',
'title' => 'Cron Testing',
'log' => 'Time : ' . date('H:i:s')
]);
}
After my files done, i run this command at my terminal to run my cron:
我的文件完成后,我在我的终端运行这个命令来运行我的 cron:
php artisan schedule:run
After i run schedule artisan command, then my terminal show this message :
在我运行 schedule artisan 命令后,我的终端显示此消息:
Running scheduled command: '/usr/bin/php5' 'artisan' removeUnpaidInvoice > '/dev/null' 2>&1 &
I think it's work, then i check my database to look the userlog is created or not, and it's created, the user log is added new via cron.
我认为这是可行的,然后我检查我的数据库以查看用户日志是否已创建,并且已创建,用户日志是通过 cron 添加的。
But the problem is, i wait for one minute, and no userlog added, wait for 2 minute, 3 minute and more, there's no another userlog added to my database?
但问题是,我等了一分钟,没有添加用户日志,等 2 分钟、3 分钟甚至更多,我的数据库中没有添加其他用户日志?
How to fix it? am i made a mistake??
如何解决?我弄错了吗??
回答by Andrey Lutskevich
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, >Laravel evaluates your scheduled tasks and runs the tasks that are due.
这是您需要添加到服务器的唯一 Cron 条目:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
这个 Cron 每分钟都会调用 Laravel 命令调度程序。然后,>Laravel 评估您的计划任务并运行到期的任务。
You need start the cron, no run php artisan schedule:run
in the console.
您需要启动 cron,而不是php artisan schedule:run
在控制台中运行。