Laravel “schedule:work” Command

News

October 7th, 2020

schedulework.jpg

Laravel has an Ideas Github area where you can use the issue tracker to propose your ideas for future changes to the framework, and a few weeks ago Brian Dillingham threw out the idea to improve how schedule:run works locally. Mainly when you want to test your schedules without dealing with CRON locally.

From this discussion, Illia Sakovich submitted a pull request to add a brand new “schedule:work” command and it’s now merged and in the latest Laravel release.

schedule:work

The new schedule:work command mimics an every minute CRON Tab and calls schedule:run directly every minute, and here is the code that drives it:

1public function handle()
2{
3 $this->info('Schedule worker started successfully.');
4
5 while (true) {
6 if (Carbon::now()->second === 0) {
7 $this->call('schedule:run');
8 }
9
10 sleep(1);
11 }
12}

It’s simple and pretty smart. Basically it keeps running and each minute when the seconds are 0 it fires schedule:run. Then once you are done with your testing just cancel it on the terminal with Ctrl+c.

This will be a big help for testing all your scheduled commands locally.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.