laravel 9

Laravel 9 Cron Job Task Scheduling Tutorial

Pinterest LinkedIn Tumblr

In this tutorial, I will show you, how you can integrate a Laravel 9 Cron Job Task Scheduling. I have written step by step instructions of Laravel 9 Cron Job Task Scheduling

Step 1 – Install laravel 9 App
Step 2 – Create New Command
Step 3 – Register as Task Scheduler
Step 4: Run Scheduler Command For Test

Step 5: Laravel Set CronJob on live server

Step 1 – Install laravel 9 App

We need to run the command to create Laravel 9 projects.

composer create-project --prefer-dist laravel/laravel laravelCrobJob

Step 2 – Create New Command

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

app/Console/Commands/TestCron.php

<?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 = 'demo: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("Cron is working fine!");
     
        /*
           Write your database logic we bellow:
           Item::create(['name'=>'hello new']);
        */
    }
}

Step 3: Register as Task Scheduler

You can see the following scheduler methods:

->everyMinute();Run the task every minute
->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 mins past the hour
->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 week
->weeklyOn(1, ‘8:00’);Run the task every week on Tuesday at 8:00
->monthly();Run the task every month
->monthlyOn(4, ’15:00′);Run the task every month on the 4th at 15:00
->quarterly();Run the task every quarter
->yearly();Run the task every year
->timezone(‘America/New_York’);Set the timezone

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');
}
}

Step 4: Run Scheduler Command For Test

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

php artisan schedule:run

Step 5: Laravel Set CronJob on live server

If you want to schedule the task on a 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
Faster PHP Cloud Hosting