在 Laravel 中安排多个 cron
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32912774/
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
Schedule multiple crons in Laravel
提问by nirvair
I am trying to run multiple jobs in Laravel at different time.
我试图在不同时间在 Laravel 中运行多个作业。
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')
->dailyAt('08:00');
$schedule->command('emails:evening-status')
->dailyAt('17:00');
$schedule->command('email:weekly-report')
->weekly()->mondays()->at('08:00');
}
The problem here is, only the first command - emails:send
- works. The other commands don't work at the specified time. However, when I run them manually they work fine.
这里的问题是,只有第一个命令 - emails:send
- 有效。其他命令在指定时间不起作用。但是,当我手动运行它们时,它们工作正常。
What is it that I am missing?
我错过了什么?
回答by Sam Killen
I think you may need to look at your actual server cron configuration. Laravel scheduler requires the server cron to be run every minute for all schedules to run at the desired time. You may just have the cron running at 8.00am every day instead of every minute.
我认为您可能需要查看您的实际服务器 cron 配置。Laravel 调度器要求服务器 cron 每分钟运行一次,以便所有调度在所需时间运行。您可能只是让 cron 每天早上 8 点运行,而不是每分钟运行一次。
It may be worth a check, I know I had difficulty with the same issue when I first started scheduling.
这可能值得一试,我知道我第一次开始安排时遇到了同样的问题。
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
使用调度程序时,您只需要将以下 Cron 条目添加到您的服务器。如果您不知道如何将 Cron 条目添加到您的服务器,请考虑使用 Laravel Forge 之类的服务,它可以为您管理 Cron 条目:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
这个 Cron 每分钟都会调用 Laravel 命令调度程序。当 schedule:run 命令被执行时,Laravel 会评估你的计划任务并运行到期的任务。
回答by Raheel Hasan
Similar situation, except that I can calling a method for each cron using call
, for example:
类似的情况,除了我可以使用 为每个 cron 调用一个方法call
,例如:
$schedule->call('\App\Http\Controllers\comm\commCron::abc');
$schedule->call('\App\Http\Controllers\comm\commCron::xyz');
I had the same problem, later realised first cron was calling an exit
at some point of error.
我遇到了同样的问题,后来意识到第一个 cronexit
在某个错误点调用了 an 。
So please make sure none of the crons retuls in exit
or die()
. Just return false
instead (if you need to).
所以请确保没有任何 cron 返回exit
或die()
。只是return false
(如果需要)。
回答by AP-
We ran intro similar issues when using Laravel's built-in scheduling module.
我们在使用 Laravel 的内置调度模块时遇到了类似的问题。
In our case, the first command was silently failing and preventing the subsequent scheduled commands from being run.
在我们的例子中,第一个命令默默地失败并阻止了后续预定命令的运行。
As a quick test, try changing the order of the commands such that emails:send is at the bottom and see if that works. In general, catch any Exceptions that may be thrown and it should fix the issue.
作为快速测试,尝试更改命令的顺序,使 email:send 位于底部,看看是否有效。一般来说,捕获可能抛出的任何异常,它应该可以解决问题。
回答by fico7489
If second comand is not working:
如果第二个命令不起作用:
$schedule->command('emails:evening-status')
->dailyAt('17:00');
you can insert only one line of code that print current date in some file eg. "test.txt" then check in wanted time if file is created. If file is created you can conclude that something if wrong with code in script that is scheduled.
您只能在某个文件中插入一行打印当前日期的代码,例如。“test.txt”然后在创建文件时检查所需的时间。如果创建了文件,您可以得出结论,如果调度的脚本中的代码有问题。
回答by Rikal
Use queue for job processing
使用队列进行作业处理
on cron, add all jobs to queue
在 cron 上,将所有作业添加到队列中
Run multiple queue workers
运行多个队列工作者
回答by AddWeb Solution Pvt Ltd
You can try below code:
你可以试试下面的代码:
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send')
->dailyAt('08:00')->withoutOverlapping();
$schedule->command('emails:evening-status')
->dailyAt('17:00')->withoutOverlapping();
$schedule->command('email:weekly-report')
->weekly()->mondays()->at('08:00')->withoutOverlapping();
}
Above code just execute without overlapped. You can get more idea about prevent overlapping.
上面的代码只是执行而不重叠。您可以获得更多关于防止重叠的想法。
Let me know if you still facing same.
如果您仍然面临同样的问题,请告诉我。
回答by meisam masomi
if you need use Laravel Schedule Use This code
如果您需要使用 Laravel Schedule 使用此代码
$schedule = new Schedule();
$schedule->command('emails:send')
->dailyAt('08:00')->withoutOverlapping();
$schedule = new Schedule();
$schedule->command('emails:evening-status')
->dailyAt('17:00')->withoutOverlapping();
$schedule = new Schedule();
$schedule->command('email:weekly-report')
->weekly()->mondays()->at('08:00')->withoutOverlapping();
but may be this way is wrong, advise to use Laravel artisan Command and put that cammand on host cron jobs
但可能这种方式是错误的,建议使用 Laravel artisan 命令并将该命令放在主机 cron 作业上