问题
I want to run a cron just once at a custom date & time entered by the user in a form. What is the best way to do this?
I found that a custom cron can be scheduled in laravel like this
->cron(‘* * * * * *’);
Run the task on a custom Cron schedule.
But I could not find the time format what the * mean.
Or much simpler, can it be done like this by adding the date and time.
->at('28/04/2020 13:00');
How can this be done?
回答1:
you can easily do that with when() method
$schedule->command('command')->when(function (){
return Carbon::create(2020,4,28,13)->isPast();
});
回答2:
Laravel documentation has in detail description on that in here Task Scheduling
You can add your scheduling logic in a given file
App\Console\Kernel
Currently, laravel have the kind of functionality that exactly match your requirement but it provide a ->monthlyOn(23, '13:00');
via which you can achieve your requirement, It Run the task every month on the 23th at 13:00
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
})->->monthlyOn(23, '13:00');
}
The above code runs the task every month on the 23th at 13:00 hours server time.
For your purpose you can change cron scheduling as given below
00 13 28 APR Fri *
1st * Minute
2nd * Hour
3rd * Day of the Month
4th * Month of the Year
5th * Day of the Week
6th * Year
来源:https://stackoverflow.com/questions/61094820/one-time-custom-cron-schedule-in-laravel