News

Laravel “schedule:work” Command

Published
Laravel “schedule:work” Command image

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:

public function handle()
{
$this->info('Schedule worker started successfully.');
 
while (true) {
if (Carbon::now()->second === 0) {
$this->call('schedule:run');
}
 
sleep(1);
}
}

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.

Eric L. Barnes photo

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

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article