Sub-Minute and Cron-less Scheduled Tasks in Laravel

Published on by

Sub-Minute and Cron-less Scheduled Tasks in Laravel image

The Spatie team released two new packages around scheduled tasks: the laravel-cronless-schedule package and laravel-short-schedule. While these packages have different use-cases, we thought they were related enough to share them in one post.

Typically the native Laravel scheduling gives you enough flexibility for most apps; however, these packages provide both an excellent development flow and advanced features when you need to run scheduled tasks more often than every minute.

Scheduled Tasks without Cron

The laravel-cronless-schedule package uses a ReactPHP loop to run the scheduler without relying on cron. If you’re developing locally, the “cron-less” scheduler could come in handy to run scheduled tasks without setting up cron:

php artisan schedule:run-cronless

According to the documentation, “This command will never end. Behind the scenes, it will execute php artisan schedule every minute.”

Freek Van der Herten explains in his blog post, A package to run the Laravel scheduler without relying on cron, why you should consider using this package in your development workflow:

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.

This package avoids all the platform-specific scheduling issues, and also includes some useful flags:

# Run the scheduler every five seconds
php artisan schedule:run-cronless --frequency=5
 
# Run a custom command
php artisan cronless-schedule:run --command=your-favorite-artisan-command
 
# Stop running the scheduler after x seconds
php artisan cronless-schedule:run --stop-after-seconds=5

As an aside, I wrote about Running the Laravel Scheduler and Queue with Docker that runs the scheduler command in a separate container. However, it can be a pain if you want to run the scheduler docker container in development selectively.

You can find the source code for laravel-cronless-schedule on GitHub at spatie/laravel-cronless-schedule.

Laravel Short Schedule

The second package Spatie released is laravel-short-schedule, which enables running the Laravel scheduler at sub-minute frequencies. Similar to the cronless-schedule package, it uses a ReactPHP event loop.

Here’s an example of what you can do with this package by defining a shortSchedule() method on an app’s console Kernel class:

use \Spatie\ShortSchedule\ShortSchedule;
protected function shortSchedule(ShortSchedule $shortSchedule)
{
// this command will run every second
$shortSchedule->command('artisan-command')->everySecond();
 
// this command will run every 30 seconds
$shortSchedule->command('another-artisan-command')->everySeconds(30);
 
// this command will run every half a second
$shortSchedule->command('another-artisan-command')->everySeconds(0.5);
}

Like the native Laravel scheduler, you can schedule commands to run between two times and avoid overlapping tasks if a task is still running.

You can learn more about this package by checking out Freek’s blog post: A package to schedule Artisan commands at sub-minute frequencies. You can find the source code on GitHub at spatie/laravel-short-schedule.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
Laravel Forge logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Sort Elements with the Alpine.js Sort Plugin image

Sort Elements with the Alpine.js Sort Plugin

Read article
Anonymous Event Broadcasting in Laravel 11.5 image

Anonymous Event Broadcasting in Laravel 11.5

Read article
Microsoft Clarity Integration for Laravel image

Microsoft Clarity Integration for Laravel

Read article
Apply Dynamic Filters to Eloquent Models with the Filterable Package image

Apply Dynamic Filters to Eloquent Models with the Filterable Package

Read article
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Asserting Exceptions in Laravel Tests image

Asserting Exceptions in Laravel Tests

Read article