Get started with 33% off your first certification using code: 33OFFNEW

A guide to all available scheduling schedules in Laravel

3 min read
Published on 1st February 2024

Laravel's task scheduling is a powerful feature that simplifies the management of periodic tasks such as sending out emails, cleaning up databases, or generating reports. Instead of creating a Cron job for each task, Laravel allows you to define your schedule within your application, providing a more readable and maintainable approach. This article provides an overview of all available scheduling schedules in Laravel.

Basics of Laravel Scheduling

Laravel's scheduler is defined in the app/Console/Kernel.php file. The scheduler offers a fluent API to expressively define the frequency of commands and tasks. To enable it, a single Cron entry on your server is required, which executes the Laravel scheduler every minute:

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

Available Scheduling Frequencies

Laravel offers a variety of scheduling frequencies, allowing for granular control over task execution. Here are the most commonly used options:

1. ->everyMinute();

The task will run every minute:

$schedule->command('emails:send')->everyMinute();

2. ->everyTwoMinutes();

To run a task every two minutes:

$schedule->command('emails:send')->everyTwoMinutes();

3. ->everyFiveMinutes();

For tasks that should occur every five minutes:

$schedule->command('emails:send')->everyFiveMinutes();

4. ->everyTenMinutes();

To schedule a task every ten minutes:

$schedule->command('emails:send')->everyTenMinutes();

5. ->everyFifteenMinutes();

To run a task every fifteen minutes:

$schedule->command('emails:send')->everyFifteenMinutes();

6. ->everyThirtyMinutes();

For tasks that should run every thirty minutes:

$schedule->command('emails:send')->everyThirtyMinutes();

7. ->hourly();

To run a task at the beginning of every hour:

$schedule->command('report:generate')->hourly();

8. ->hourlyAt(17);

Run a task at a specific minute past the hour:

$schedule->command('report:generate')->hourlyAt(17);

9. ->daily();

For tasks that should run once a day:

$schedule->command('database:backup')->daily();

10. ->dailyAt('13:00');

To run a task every day at a specific time:

$schedule->command('reminder:send')->dailyAt('13:00');

11. ->twiceDaily(1, 13);

To run a task twice a day at specific times:

$schedule->command('report:generate')->twiceDaily(1, 13);

12. ->weekly();

For tasks that should run once a week:

$schedule->command('emails:clean')->weekly();

13. ->weeklyOn(1, '8:00');

Run a task on a specific day and time of the week (0 = Sunday, 1 = Monday, etc.):

$schedule->command('report:weekly')->weeklyOn(1, '8:00');

14. ->monthly();

To run a task once a month:

$schedule->command('billing:generate')->monthly();

15. ->monthlyOn(4, '15:00');

Run a task on a specific day of the month at a specific time:

$schedule->command('subscription:renew')->monthlyOn(4, '15:00');

16. ->quarterly();

For tasks that should run every quarter:

$schedule->command('report:quarterly')->quarterly();

17. ->yearly();

To run a task once a year:

$schedule->command('audit:yearly')->yearly();

18. ->weekdays();

Run a task every weekday:

$schedule->command('report:daily')->weekdays();

19. ->weekends();

For tasks to run on weekends:

$schedule->command('server:restart')->weekends();

Combining Constraints

You can combine multiple time constraints for a single task:

$schedule->command('report:generate')
         ->weekdays()
         ->at('23:00');

Laravel's scheduling system provides a robust and fluent way to manage timed tasks in your application. With a wide range of scheduling options, you can tailor tasks to run precisely when needed, keeping your application efficient and responsive. By learning these scheduling capabilities, you can ensure your Laravel application performs optimally with minimal manual intervention.