问题
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?
回答1:
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');
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();
Another ways (Laravel 5.4 onwards) onliner to schedule the event to run twice monthly.
$schedule->command('payments:create')->twiceMonthly( 1, 15);
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
Another way to excute Crontab at midnight of every 1 and 15 of every month
$schedule->command('payments:create')->cron('0 5 1,15 * *');
回答2:
You can use ->mondays() to ->sundays() for specific days every week.
Then use ->at('23:59') for specific time of the day.
来源:https://stackoverflow.com/questions/51398476/laravel-scheduler-run-specific-days-of-month-at-specific-time