Laravel Cron Job Task Scheduling Tutorial with Example

Throughout the Laravel 9 cron job task scheduling example tutorial, you will learn how to create and setup cron job task scheduling in laravel application.

Laravel provides a convenient way to schedule cron jobs by scheduling a single schedule:run Artisan command to run every minute. The schedule:run command will examine the job schedule defined in your App\Console\Kernel class to determine which scheduled tasks to run.

Laravel Cron Job Example

Theoretically, Laravel comes with built-in robust task manager, and you can leave all the tasks on its discretion. It gives the precedence to the tasks scheduled by you. It’s better if you have a Linux operating system to make off the Cron Jobs.

Follow the following steps to create and setup cron job task scheduling in laravel apps:

  • Step 1: Download Laravel App
  • Step 2: Create Cron Job Command
  • Step 3: Register Cron job
  • Step 4: Run Scheduler Command For Test
  • Step 5: Laravel Set CronJob Live Server

Download Laravel App

First create new laravel application adding the following command in terminal.

composer create-project --prefer-dist laravel/laravel laravel-cron-job

Go into the app:

cd laravel-cron-job

Create Cron Job Command

Then execute the following command on terminal to create TestCron job class:

php artisan make:command TestCron --command=test:cron

Now open the app/Console/Commands/TestCron.php file and put the below code on it:

<?php
   
namespace App\Console\Commands;
   
use Illuminate\Console\Command;
   
class TestCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:cron';
    
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command for test cron job';
    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \Log::info("Cron is working fine!");
     
        /*
           Write your database logic we bellow:
           Model::create(['name'=>'hello new']);
            send email here
        */
    }
}

Register as Task Scheduler

In this step, register the above created cron job class in kernel.php file.

So, navigate to app/Console directory and open Kernel.php. Then register cron job command like following:

app/Console/Kernel.php

<?php
   
namespace App\Console;
    
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\TestCron::class,
    ];
     
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('test:cron')
                 ->everyMinute();
    }
     
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
     
        require base_path('routes/console.php');
    }
}

In Kernel.php file you can schedule the task to be done when it will be the command execute(run).

You can see the following scheduler methods:

MethodDescription
->cron('* * * * *');Run the task on a custom cron schedule
->everyMinute();Run the task every minute
->everyTwoMinutes();Run the task every two minutes
->everyThreeMinutes();Run the task every three minutes
->everyFourMinutes();Run the task every four minutes
->everyFiveMinutes();Run the task every five minutes
->everyTenMinutes();Run the task every ten minutes
->everyFifteenMinutes();Run the task every fifteen minutes
->everyThirtyMinutes();Run the task every thirty minutes
->hourly();Run the task every hour
->hourlyAt(17);Run the task every hour at 17 minutes past the hour
->everyTwoHours();Run the task every two hours
->everyThreeHours();Run the task every three hours
->everyFourHours();Run the task every four hours
->everySixHours();Run the task every six hours
->daily();Run the task every day at midnight
->dailyAt('13:00');Run the task every day at 13:00
->twiceDaily(1, 13);Run the task daily at 1:00 & 13:00
->weekly();Run the task every Sunday at 00:00
->weeklyOn(1, '8:00');Run the task every week on Monday at 8:00
->monthly();Run the task on the first day of every month at 00:00
->monthlyOn(4, '15:00');Run the task every month on the 4th at 15:00
->twiceMonthly(1, 16, '13:00');Run the task monthly on the 1st and 16th at 13:00
->lastDayOfMonth('15:00');Run the task on the last day of the month at 15:00
->quarterly();Run the task on the first day of every quarter at 00:00
->yearly();Run the task on the first day of every year at 00:00
->yearlyOn(6, 1, '17:00');Run the task every year on June 1st at 17:00
->timezone('America/New_York');Set the timezone for the task

Run Scheduler Command For Test

In this step, execute the following command on terminal to run scheduler:

php artisan schedule:run

storage/logs/laravel.php

[2019-04-24 03:46:42] local.INFO: Cron is working fine!  
[2019-04-24 03:46:52] local.INFO: Cron is working fine!  
[2019-04-24 03:46:55] local.INFO: Cron is working fine!  

Laravel Set CronJob on live server

If you want to schedule the task on live server use the below command :

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

Laravel 9 cron job task scheduling example; we have successfully created cronjob and also how to run scheduler in laravel app.

Leave a Comment