dev0010
dev0010

Reputation: 95

Laravel Scheduler: Run every x hours except for given times

I know we can specify certain tasks for every X individual times like following will run after every two hours:

$schedule->command('catalog:update')->cron('0 */2 * * *');

However, I need to run every two hour but still be able to exclude certain times. For example, Scheduler should run my task every two hours but not between these times: 12 am to 5 am

I was thinking of putting current time check login in task itself which might work. Is there any other better or laravel-specific way of doing it ?

Thanks for the help/suggestions

Upvotes: 5

Views: 7305

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

Use Laravel Scheduler with hourly() and unlessBetween().

The unlessBetween method can be used to exclude the execution of a task for a period of time

An example:

->hourly()->unlessBetween('00:00', '5:00');

https://laravel.com/docs/5.5/scheduling#schedule-frequency-options

Upvotes: 11

Sagar Gautam
Sagar Gautam

Reputation: 9389

In order to execute command except at duration between 12 am to 5 am just think reverse of it. Execute the command only between 5 am to 12 am using between keyword.

You can use between which limit the task to run between start and end times like

$schedule->command('test')->cron('0 */2 * * *')->between('5:00', '00:00');

Upvotes: 1

Related Questions