DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

How to Run a Cron Job Every 5 Minutes in Laravel 11

Laravel provides a powerful scheduling system to automate tasks, but to make the most of it, you need to configure your server’s cron jobs properly. By default, many setups use a cron job that runs every minute, but what if you want to run it every 5 minutes? This article will guide you through the process of setting up a cron job to run every 5 minutes in Laravel.

What is a Cron Job?

A cron job is a time-based task scheduler in Unix-like operating systems (like Linux or macOS). It allows users to schedule scripts or commands to run at specified intervals. In Laravel, cron jobs are used to trigger the Laravel scheduler, which manages all the scheduled tasks in your application.

Why Run a Cron Job Every 5 Minutes?

Running a cron job every minute can be overkill for certain applications, especially if the scheduled tasks themselves only need to run every 5 minutes or more. Running it every 5 minutes can reduce the load on your server and make the system more efficient while still ensuring tasks are executed on time.

Step 1: Configure Laravel Scheduler

Laravel’s task scheduling feature allows you to define scheduled tasks in app/Console/Kernel.php for laravel 10 and below and routes/console.php for laravel 11. The scheduler itself needs to be triggered by a cron job to work, and by default, many developers set it up to run every minute.

Here’s an example of a scheduled task in Laravel:

<?php

use Illuminate\Foundation\Inspiring;
use App\Services\ApiMonitoringService;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();

Schedule::command('jobs:fetch-update')->hourly();

Enter fullscreen mode Exit fullscreen mode

In this example, the jobs:fetch-update command is set to run every hour. However, the cron job itself might be running every minute, and we want to change it to every 5 minutes.

Step 2: Edit the Crontab

The cron job is what triggers the Laravel scheduler. To modify the cron job to run every 5 minutes, follow these steps:

Open the Crontab

To edit the cron job, you need to access the crontab on your linux server. Run the following command to open the crontab:

crontab -e
Enter fullscreen mode Exit fullscreen mode

This will open the crontab file in your server's default text editor.

Modify the Cron Job Entry

In the crontab file, locate or add the cron job that runs Laravel’s scheduler. You will need to modify it to run every 5 minutes instead of every minute.

Replace:

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

With:

*/5 * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Enter fullscreen mode Exit fullscreen mode

Breakdown of the Cron Expression:

  • */5: This tells the cron to run every 5 minutes.
  • * * * *: The remaining asterisks mean "every hour, every day, every month, every weekday."
  • php artisan schedule:run: This runs Laravel's scheduler, which will check if any tasks are due to run.
  • >> /dev/null 2>&1: This discards any output or errors that may occur during the execution.

Step 3: Save and Exit

Once you've modified the cron job entry, save the file and exit the editor.

  • In nano, you can press CTRL+O to save and CTRL+X to exit.
  • In vim, press ESC, type :wq, and hit Enter to save and quit.

Step 4: Laravel Scheduler Behavior

With the cron job now running every 5 minutes, Laravel’s schedule:run command will check for scheduled tasks every 5 minutes. However, it will only run tasks when they are due, based on how you’ve scheduled them in the console.php file.

For example:

  • If a task is scheduled to run hourly (Schedule::command('jobs:fetch-update')->hourly();), it will still only run once every hour, even though the cron job checks every 5 minutes.
  • If a task is scheduled to run every 5 minutes (Schedule::command('jobs:fetch-update')->everyFiveMinutes();), it will now run every 5 minutes.

Example of an Every-5-Minute Task in Laravel

If you want to ensure that a task runs every 5 minutes, you can define it in Kernel.php like this:

Schedule::command('jobs:fetch-update')->everyFiveMinutes();
Enter fullscreen mode Exit fullscreen mode

This task will now be executed every 5 minutes when the cron job runs.

Step 5: Verify the Cron Job is Running Every 5 Minutes

You can verify that the cron job is running as expected by adding a simple logging task to your Laravel scheduler. For example, in routes/console.php:

Schedule::command('jobs:fetch-update')->everyFiveMinutes();
Enter fullscreen mode Exit fullscreen mode

This will write a log entry in storage/logs/laravel.log every time the scheduler runs.

Alternatively, you can redirect cron job output to a log file to track when the schedule:run command is executed:

*/5 * * * * cd /path-to-your-project && php artisan schedule:run >> /path-to-output.log 2>&1
Enter fullscreen mode Exit fullscreen mode

Check the output log to see if the cron job runs every 5 minutes.

Conclusion

By setting your cron job to run every 5 minutes, you ensure that Laravel’s scheduler checks for due tasks more efficiently. The Laravel scheduler will still only execute tasks according to the intervals you’ve set (e.g., hourly, daily), but it will check more frequently. This method optimizes your server load and ensures your application remains responsive to your scheduling needs.

Key steps:

  1. Modify the cron job to run every 5 minutes (*/5 * * * *).
  2. Define tasks in Kernel.php using Laravel’s scheduling methods (hourly(), everyFiveMinutes(), etc.).
  3. Optionally, use logging to confirm the scheduler runs as expected.

This approach allows you to balance performance and task execution frequency in Laravel.

Top comments (0)