Laravel调度程序与Cron工作 [英] Laravel Scheduler with Cron Job
问题描述
我使用larave 5.1与php5,我尝试创建我的cron工作删除未付的发票在我想要,但我测试它打印一个用户日志,以帮助我知道工作是工作。
这是我的应用程序/ Console / 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();
}
这是我的RemoveUnpaidInvoice类:
public function handle()
{
UserLog :: create([
'user_id'=>'3',
'title'=>'Cron Testing',
'log'=>'Time:'。date('H:i:s')
]
}
在我的文件完成后,我在终端运行这个命令运行我的cron :
php artisan schedule:run
运行schedule artisan命令后,我的终端显示以下消息:
:'/ usr / bin / php5''artisan'removeUnpaidInvoice> '/ dev / null'2>& 1&
我认为它的工作,然后我检查我的数据库看看userlog是否创建,
但问题是,我等了一分钟,没有添加用户日志,等待2分钟,3分钟和更多,没有其他用户记录添加到我的数据库?
如何解决?我做错了吗
这里是您需要添加到服务器的唯一Cron条目: / p>
* * * * * php / path / to / artisan schedule:run> / dev / null 2>& 1
此Cron将每分钟调用Laravel命令调度程序。然后,> Laravel评估您的预定任务,并运行到期的任务。
您需要启动cron, $ c> php artisan schedule:run 。
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.
This is my app/Console/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 :
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:
php artisan schedule:run
After i run schedule artisan command, then my terminal show this message :
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.
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?
How to fix it? am i made a mistake??
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.
You need start the cron, no run php artisan schedule:run
in the console.
这篇关于Laravel调度程序与Cron工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!