Laravel Scheduler-在特定时间运行每月的特定日期 [英] Laravel scheduler - Run specific days of month at specific time
问题描述
我想在每月的特定日期特定时间运行任务. 现在,我使用此代码在每月的1号和15号运行任务.
I want to run a task at specific days of month in specific time. Now i use this code to run the task on 1st and 15th day of the month.
$schedule->command('payments:create')->daily()->when(function ()
{
$days = [1,15];
$today = Carbon::today();
return in_array($today->day, $days);
});
有什么方法可以将此任务设置为在特定时间运行?
Is there any way to set this task to run on these days at specific time?
推荐答案
您可以通过以下两种方法以多种方式实现它:
You can achieve it in many ways, following two liner :
$schedule->command('payments:create')->monthlyOn(1, '15:00');
$schedule->command('payments:create')->monthlyOn(15, '15:00');
如果您想强制任务甚至在维护模式下运行,则可以使用 evenInMaintenanceMode 方法:
if you would like to force a task to run even in maintenance mode, you may use the evenInMaintenanceMode method:
$schedule->command('payments:create')->monthlyOn(15, '15:00')->evenInMaintenanceMode();
$schedule->command('payments:create')->monthlyOn(1, '15:00')evenInMaintenanceMode();
另一种方式(从Laravel 5.4起)可以将活动安排为每月两次.
Another ways (Laravel 5.4 onwards) onliner to schedule the event to run twice monthly.
$schedule->command('payments:create')->twiceMonthly( 1, 15);
如果要在特定时间运行,请使用-> at();.方法.
If you want to run at specific time then use ->at(); method.
$schedule->command('payments:create')->twiceMonthly( 1, 15)->at('13:00');// At 1 PM of every 1st and 15th of every month
另一种方法是在每月的每个1和15的午夜执行Crontab
Another way to excute Crontab at midnight of every 1 and 15 of every month
$schedule->command('payments:create')->cron('0 5 1,15 * *');
这篇关于Laravel Scheduler-在特定时间运行每月的特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!