Cron Job Task Scheduling In Laravel 9

In this article, we will see cron job task scheduling in laravel 9. Many times we require to run some piece of code with a specific interval time period in laravel and we need to run it manually every time but the command scheduler through we can run and create a cron job in laravel 9. So, here we will learn how to create a cron job in laravel 9 and how to create custom commands in laravel 9.

Laravel's command scheduler offers a fresh approach to managing scheduled tasks on your server. When using the scheduler, only a single cron entry is needed on your server.

So, let's see laravel 9 cron job task scheduling, how to create a cron job in laravel 9, and create custom commands in laravel 9.

How To Create Cron Job Task Scheduling In Laravel 9

Step 1: Install Laravel 9

Step 2: Create Cron Job Command

Step 3: Register Cron Job Command

Step 4: Run Scheduler Command

Step 5: Laravel Set Cron Job on Live Server

 

First of all, you need to create a custom command. it will execute with the task scheduling cron job.

php artisan make:command TestingCron --command=testing:cron

 

 

Now add the below code in the TestingCron.php file.

app/Console/Commands/TestingCron.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestingCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'testing:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
        \Log::info("Testing Cron is Running ... !");
     
        /*
           Write your database logic we bellow:
           User::create(['email'=>'send mail']);
        */
      
        $this->info('testing:cron Command Run Successfully !');
    }
}

 

 

Now, in the kernel file, we will set a specific time interval to run the cron job.

<?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\TestingCron::class,
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('testing:cron')->everyMinute();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

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

there are many more task schedule frequencies that you may assign to a task.

Method Description
->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


Now, We are ready to run the cron job scheduler command. So, run the below artisan command in your terminal.

php artisan schedule:run

And you will check your log file there will be a display output that one already set by us.

 

 

Here we will set up the cron job command on the server. 

* * * * * 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

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS