Laravel 6 Cron Job Task Scheduling Tutorial

  • 03-04-2022
  • 1055
  • Laravel 6
  • Haresh Chauhan

If you have any queries on Laravel 6 CronJob Task scheduling. So this post for you. How to create CRONJOB? , or How to set task scheduling and how to use this cronjob many questions I have solved in this post. In this tutorial, we will learn how to make task scheduling and how to use the Cron Job. I give a solution to many questions related to this task scheduling and as well as how to set up on the live server. So, I will explain to you step by step.

What is the actual use of the CRONJOB?

If you have this question then I will explain. Many time need to send notification or send email automatically to the user for update properly. You can set a cronjob for your requirements like every hour, every minute, every day. Then we need to use cronjob. Because it's an easy way to do this task.

You can also cronjob set on your server and use. If you want to do this then follow the below steps.

Laravel 6 Cron Job Task Scheduling Tutorial

Step 1 : Install Laravel 6 Application.

In this step, we require fresh Laravel 6 Application. So, follow the below command and get the Laravel 6 Application.

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

Step 2 : Create Command For Task Scheduling.

Now, In this step, we need to create command for Task Scheduling. So, let's do this below command through.

php artisan make:command Update --command=update:info

Now, we make some change on Update command file.

app/Console/Commands/Update.php

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

class Update extends Command

{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */

    protected $signature = 'update:info';

    /**
     * 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("CronJob is working!");

        /*
           Write your database logic we bellow:
        
           User::create(['name'=>'Dharmesh Chauhan']);
        
        */

        $this->info('update command run successfully!');
    }

}

3 : Register as Task Scheduler like this wayStep

In this step, we know the few functions of the task scheduler Laravel 6 Application.

-> 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(14); Run the task every hour at 14 mins past the hour
-> daily(); Run the task every day at midnight
-> dailyAt(’16:00′); Run the task every day at 16:00
-> twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
-> weekly(); Run the task every week
-> weeklyOn(1, ‘10:00’); Run the task every week on Monday at 10:00
-> monthly(); Run the task every month
-> monthlyOn(6, ’13:00′); Run the task every month on the 6th at 13:00
-> quarterly(); Run the task every quarter
-> yearly(); Run the task every year
-> timezone(‘America/New_York’); Set the timezone

You can set cronjob using the above function and you can customize as per your need.

Now, we must need to register this command on the kernel.php file. So let's do this.

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

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */

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

    /**
     * Register the commands for the application.
     *
     * @return void
     */

    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');
        require base_path('routes/console.php');
    }
}

4: Run Scheduler CommandStep

Now, We test scheduler command like two ways.

Method 1

php artisan update:info

Method 2

php artisan schedule:run

After run the command and check the log file on storage/logs/laravel.php file. So, Open and check the file and showing log printed code.

[2019-11-13 10:12:26] local.INFO: CronJob is working!
[2019-11-13 10:13:27] local.INFO: CronJob is working! 
[2019-11-13 10:14:27] local.INFO: CronJob is working!
[2019-11-13 10:15:28] local.INFO: CronJob is working!
[2019-11-13 10:16:28] local.INFO: CronJob is working!

You can set the below command cronjob in the live server.

Using crontab -e command and set your cronjob in live server.

This command mostly uses in Cpanel, If you want to use shared hosting than set a minimum of fifteen minutes otherwise isn't working. It's a limitation from the hosting side.

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

This command use in mostly cloud hosting this commmand put into file.

crontab -e Run the command on live server and put below command into open file and save.

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

If you don't understand how to set command then follow this link and make scheduler command. Crontab Guru

I hope you found the best.

Thanks...


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :