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.

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

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

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
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant 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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article