Thursday, March 19, 2020

How to set up Task Scheduling with Cron job in Laravel ?

How to set up Task Scheduling with Cron job in Laravel ?



Hello Buddies,

Do you have a question? how to set up task scheduling with cron job in laravel? How do I use task planning in Laravel 7/6? then I'll give you a simple example of programming cron job tasks with Laravel 7/6. I will create a step-by-step tutorial on how to configure the cron job using the Laravel 7/6 task plan. You can also configure the cron job on your server.

Why do we have to use cron job? And what is the advantage of using cron jobs in Laravel 6 and how to configure cron jobs in Laravel 6? If you have this question, I will explain why. We often have to automatically send notifications or emails to users to update properties or products. At this point, you can define the basic logic for every day, hour, etc. It can run and send email notifications.

You also want to set up a cron job on your server. You can then follow this tutorial and put it into practice.

Step 1: Install Laravel 7/6


If you did not configure the Laravel 6 app in this step, we need a new Laravel 6 app. So run the following command and get a clean and fresh Laravel 6 app.

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


Step 2: Create New Command


In this step we need to create our custom command. The custom command runs with the Scron job scheduling task. Therefore, run the following command to create a new custom command.

php artisan make:command DemoCron --command=demo:cron

Now make some changes to the command file.

app/Console/Commands/DemoCron.php
<?php

   

namespace App\Console\Commands;

   

use Illuminate\Console\Command;

   

class DemoCron 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']);

        */

      

        $this->info('Demo:Cron Cummand Run successfully!');

    }

}


Step 3: Register as Task Scheduler


In this step we need to define our commands in the Kernel.php file with the time you want your command to run as follows:


->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\DemoCron::class,

    ];

     

    /**

     * Define the application's command schedule.

     *

     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule

     * @return void

     */

    protected function schedule(Schedule $schedule)

    {

        $schedule->command('demo: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


We are now ready to run our cron so you can check it manually from your cron using the following command. Then run the following command:

php artisan schedule:run

After executing the above command, you can review the log file in which text was already printed. So open your log file shown below:

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!  

  

You can finally manage this command in the planning task. You need to add a single entry to your server's crontab file:

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


I hope this tutorial how to set up task scheduling with cron job in laravel could help you .

Thaaaanks….



0 comments:

Post a Comment

Please don't enter any spam link in the comment box.