Laravel 10 Cron Job Task Scheduling Tutorial

In this tutorial, we will walk through the process of setting up a cron job in Laravel 10 using the built-in task scheduler. We will explain the steps to create a custom command and register it in the task scheduler. Also, test the scheduler command.

By following this guide, you will learn how to effectively create, manage, and execute cron jobs in Laravel 10.

Cron jobs are essential for many web applications, as they automate tasks such as sending email notifications or updating records at specific intervals.

Laravel task scheduler simplifies the management of cron jobs by allowing you to define the frequency, handle dependencies, and utilize Laravel’s built-in functionality.

 

How to Setup CRON Jobs in Laravel 10?

 

Step 1 – Install Laravel 10

Step 2 – Create a Custom Command

Step 3 – Register the Command in the Task Scheduler

Step 4- Test the Scheduler Command

Step 5 – Add the Task to the Server’s Crontab

 

 

Step 1: Install Laravel 10

First, if you do not have a Laravel 10 application set up, you will need to create one. Run the following command to create a fresh Laravel 10 installation:

composer create-project --prefer-dist laravel/laravel example-app

 

Replace example-app with the desired name for your project.

 

Step 2: Create a Custom Command

In this step, you will create a custom command that will be executed by the cron job. Run the following command to create a new custom command:

php artisan make:command ExampleCron --command=example:cron

 

Modify the generated Command file as needed:

app/Console/Commands/ExampleCron.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ExampleCron extends Command
{
    protected $signature = 'example:cron';
    protected $description = 'Example cron command';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        \Log::info("Cron job is running correctly!");

        // Add your database logic here
    }
}

 

Step 3: Register the Command in the Task Scheduler

Next, define your custom command in the Kernel.php file and specify when you want it to run:

app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    protected $commands = [
        Commands\ExampleCron::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('example:cron')
                 ->everyMinute();
    }

    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

 

You can adjust the frequency by replacing ->everyMinute() with the desired interval, such as ->daily() or ->hourly().

 

Step 4: Test the Scheduler Command

Now, you can manually test your cron job by running the following command:

php artisan schedule:run

Check the log file to verify the cron job’s execution:

storage/logs/laravel.log

[2023-04-18 03:46:42] local.INFO: Cron job is running correctly!
[2023-04-18 03:46:52] local.INFO: Cron job is running correctly!
[2023-04-18 03:46:55] local.INFO: Cron job is running correctly!

 

Step 5: Add the Task to the Server’s Crontab

Finally, to automate the task execution, add a single entry to your server’s crontab file:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Alternatively, you can use this command:

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

 

Now your Laravel 10 cron job is set up and ready to run at the specified intervals.

 

Conclusion

In this tutorial, we have demonstrated how to create and manage cron jobs in Laravel 10 using the task scheduler. By following these steps, you can automate various tasks in your Laravel application and improve its efficiency and performance.

Leave a Comment

Your email address will not be published. Required fields are marked *