Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A package to run the Laravel scheduler without relying on cron

Link –

We released a new package called spatie/laravel-cronless-schedule. It can run the Laravel scheduler (or any other command really), without relying on cron. Instead of cron, a never-ending ReactPHP loop is used.

In this blog post, I'd like to introduce the package to you.

Why not rely on cron?

Laravel's native scheduler relies on cron to be executed every minute. It's rock solid. If you use *nix machines to run your production environment, you should stick to using it.

In one of my projects, I'd like to test the behavior of one of the scheduled commands. Sure, I could manually fire off the command, but in this case, I wanted to see what the effects where if the command were to be fired on time for an extended period.

If you want to run the scheduler every minute in a local environment, using cron can be cumbersome. I bet most developers will never have touched their local crontab. There's also launchd, which can work great, but it's not as easy as just running the artisan command that spatie/laravel-cronless-schedule provides.

On Windows, cron doesn't even exist (I'm not an expert, but there you should use the Windows Scheduler). And on Docker containers, cron mostly isn't available.

Using the package

This is how you can start the cronless schedule:

php artisan schedule:run-cronless

This command will never end. Behind the scenes, it will execute php artisan schedule:run every minute.

screenshot

To perform an extra run of the scheduler, you can just press enter.

If you want to run the scheduler at another frequency, you can pass a number of seconds to the frequency option. Here is an example where the schedule will be run every 5 seconds.

php artisan schedule:run-cronless --frequency=5

By default, the command will run forever. You can shorten that period by passing a number of seconds to the stop-after-seconds option.

In this example, we'll stop the command after 120 seconds

php artisan cronless-schedule:run --stop-after-seconds=120

If you want to run another command instead of the scheduler, you can pass it to the command option. Here is an example where another command will be run.

php artisan cronless-schedule:run --command=your-favorite-artisan-command

How it works behind the scenes

The package is quite simple. The ScheduleRunCronlessCommand class that contains all functionality is less than 100 lines long.

The heart of the command consists of these two functions.

protected function scheduleCommand(): self
{
    $stopAfter = (int)$this->option('stop-after-seconds');

    if ($stopAfter > 0) {
        $this->loop->addTimer($stopAfter, fn () => $this->loop->stop());
    }

    $this->loop->addPeriodicTimer($this->frequency, fn () => $this->runCronlessCommand());

    return $this;
}

protected function runCronlessCommand()
{
    $this->comment($this->timestamp("Running {$this->command}..."));

    $this->call($this->command);

    $this->comment($this->timestamp("{$this->command} finished."));
    $this->comment('');
}

$this->loop contains a ReactPHP powered loop. The addPeriodicTimer accepts two parameters. The first one is a number of seconds. The second one is a callable. If you pass 60 to as the first argument, then the callable will be run every 60 seconds. And that is basically all there is to it.

You might wonder if ReactPHP is reliable enough for this task. Well, the library has been downloaded for millions of times already, and in my experience, it is rock solid.

If you want to see another use of a ReactPHP loop, that check out [this video], in which I explain how ReactPHP powers another cron related package of ours: spatie/laravel-short-schedule.

In closing

For my local testing needs spatie/laravel-cronless-schedule works great, and I hope this package comes in handy for you as well.

Be sure to check out this list of packages that our team has created previously.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

avatar

Well explained! its helps me allot. I have also read another blog to use Cron jobs with Laravel might be that would help someone’s query regarding Laravel hosting for cron job.

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.

Webmentions

idealPage liked on 13th June 2020
Matias Carral liked on 11th June 2020
Simpledev liked on 11th June 2020
asfak ansari retweeted on 11th June 2020
asfak ansari liked on 11th June 2020
Le Huu Phuc liked on 11th June 2020
Samer Halawani liked on 11th June 2020
Junaid Ahmad liked on 11th June 2020
Farid liked on 11th June 2020
Efrain Salas liked on 11th June 2020
Fernando Sedrez liked on 11th June 2020
Coderatio retweeted on 11th June 2020
Vaggelis Yfantis liked on 11th June 2020
Coderatio liked on 11th June 2020
Francisco Bretal liked on 11th June 2020
Alex Volley liked on 11th June 2020
Perez Cato retweeted on 11th June 2020
Perez Cato liked on 11th June 2020
Daniel liked on 11th June 2020
Ferdinand AMOI liked on 11th June 2020
Olivier Van de Velde liked on 11th June 2020
Roman Pronskiy liked on 11th June 2020
Yusuf Abayomi liked on 11th June 2020
Muhammed Andromadus liked on 11th June 2020
Luís Martins liked on 11th June 2020
Francinaldo Almeida liked on 11th June 2020
João Patrício liked on 11th June 2020
TonyStark liked on 11th June 2020
ali ali liked on 11th June 2020
Unknown liked on 11th June 2020
Himanshu Sharma liked on 11th June 2020
Carlo Garcia Paa liked on 11th June 2020
michgeek 🌀 liked on 11th June 2020
Faisal ahmed liked on 11th June 2020
Aniket Mahadik liked on 11th June 2020
Faisal ahmed replied on 11th June 2020
It's an amazing package. Freek thinks deep on how to make coding easier.
Do Hoang Dinh Tien retweeted on 11th June 2020
Ron liked on 11th June 2020
Rama Patel liked on 11th June 2020
Abu Hurairah retweeted on 11th June 2020
Abu Hurairah liked on 11th June 2020
Mango Man liked on 11th June 2020
sadam hussain liked on 11th June 2020
JJ mas não o Abrams liked on 11th June 2020
Oluwatobi Samuel Omisakin liked on 11th June 2020
Emmann liked on 11th June 2020
Irving Aguilar liked on 11th June 2020
mehRaaaaN liked on 11th June 2020
Junior Gantin liked on 11th June 2020
Sam Serrien liked on 11th June 2020
Luigi Cruz liked on 11th June 2020
Salman Zafar liked on 11th June 2020
salierdev liked on 11th June 2020
David Defoe liked on 11th June 2020
Luis M. Alvarado liked on 11th June 2020
David Defoe retweeted on 11th June 2020
Snr Apst. MAKANAKY liked on 11th June 2020
Philemon Majoni liked on 11th June 2020
Diar liked on 10th June 2020
Ahmad Ripaldi liked on 10th June 2020
Adam 🤙🏼 Mench liked on 10th June 2020
Peter Sowah liked on 10th June 2020
Olvert Leonides liked on 10th June 2020
overtrue liked on 10th June 2020
Wisdom 🚀 Osinachi retweeted on 10th June 2020
Moath liked on 10th June 2020
Niko Halink liked on 10th June 2020
ᴀʟʟʏ liked on 10th June 2020
Wisdom 🚀 Osinachi liked on 10th June 2020
Dannie L. liked on 10th June 2020
Elvis retweeted on 10th June 2020
Elvis liked on 10th June 2020
José Cage liked on 10th June 2020
Roger Vilà replied on 10th June 2020
Lol. I created this one week ago 😂 github.com/rogervila/lara…
Flamur Mavraj liked on 10th June 2020
sallm.in liked on 10th June 2020
Nasirou Wagana liked on 10th June 2020
Abhishek Jain liked on 10th June 2020
Spatie retweeted on 10th June 2020
PHP Synopsis retweeted on 10th June 2020
Adewale Toluwani retweeted on 10th June 2020
Andrée-Anne Vincent retweeted on 10th June 2020
ً retweeted on 10th June 2020
Kipyegon Josphat retweeted on 10th June 2020
Andrée-Anne Vincent liked on 10th June 2020
Adewale Toluwani liked on 10th June 2020
Bestony liked on 10th June 2020
TomBosmans4 liked on 10th June 2020
Joel Kahnwald. liked on 10th June 2020
Roberto B 🚀 liked on 10th June 2020
Kipyegon Josphat liked on 10th June 2020
ً liked on 10th June 2020
Filipe Danielski liked on 10th June 2020
Wyatt liked on 10th June 2020
Jahlien liked on 10th June 2020
Sander Versluys liked on 10th June 2020
Tyler Woonton liked on 10th June 2020
Prosper Otemuyiwa 🔥 liked on 10th June 2020
Stephen Jude liked on 10th June 2020
Eusebiu Cretu liked on 10th June 2020
GrandBlond🇨🇿 liked on 10th June 2020
Sidrit Trandafili liked on 10th June 2020
Hassan Hafeez liked on 10th June 2020
Waqar Zafar Tarar liked on 10th June 2020
Abdulfattah Alselwi liked on 10th June 2020
Léopold Jacquot liked on 10th June 2020
Mario Chamuty liked on 10th June 2020
William liked on 10th June 2020
Elo retweeted on 10th June 2020
ArielMejiaDev retweeted on 10th June 2020
Mohamed Said retweeted on 10th June 2020
ArielMejiaDev liked on 10th June 2020
Eduardo Garay Vargas liked on 10th June 2020
Levan Velijanashvili liked on 10th June 2020
Mohamed Said liked on 10th June 2020
Abrar Ahmad liked on 10th June 2020
Aman Kharbanda liked on 10th June 2020
Didik Tri Susanto liked on 10th June 2020
Yann Haefliger liked on 10th June 2020
Sadegh PM liked on 10th June 2020
Mattias Geniar liked on 10th June 2020
Federico Corsaro liked on 10th June 2020
Elo liked on 10th June 2020
Patrick Brouwers liked on 10th June 2020
Vidhya Prakash R liked on 10th June 2020
Bilal Khan retweeted on 10th June 2020
Haneef Ansari 🍭 liked on 10th June 2020
Ray Bonander liked on 10th June 2020
Kati Frantz liked on 10th June 2020
Günther Debrauwer liked on 10th June 2020
Hadi Swais liked on 10th June 2020
Bilal Khan liked on 10th June 2020
O s a m a K h a l e d liked on 10th June 2020
Ihab liked on 10th June 2020
Vaibhav Pardeshi liked on 10th June 2020
Francisco Barreto liked on 10th June 2020
Mak Man liked on 10th June 2020
@Tiago_Ferat liked on 10th June 2020
Ivan Radojevic liked on 10th June 2020
𝗈𝗌𝗆𝖾𝗅𝗅 liked on 10th June 2020
Bill Clark liked on 10th June 2020
Simon Kollross liked on 10th June 2020
Francisco Barreto replied on 10th June 2020
how is the best way to check if schedule:run-cronless is already running?