Laravel 8 Cron Job Example

By : phpicoder Created : February 25, 2021

Cron job is an important part of a web application. So this tutorial we will be focused on laravel 8 cron job setup. We will make a simple example of how to create a cron job in Laravel 8 and check step by step how it works. So, let's follow few step to create example of cron job in laravel 8 example.

In this tutorial, we will Making a Cron Job in Laravel 8 is simple and easy. And gives you a simple example of how to create a cron job in Leravel 8.

Cron is a time-based job scheduler in computer operating systems such as Unix. Users who set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals.

Step 1: Install Laravel App

First we need laravel application so open command prompt and run below command.

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

Step 2: Create New Cron Command

Now, we will create a custom command. The custom command will execute the task scheduling scron with the job. So, let's create a new custom command for which follow the steps below.

php artisan make:command FirstCron --command=first:cron

Now go to app/Console/Commands/ path and then we have a FirstCron.php file. So open this file and edit bellow code.

<?php
   
namespace App\Console\Commands;
   
use Illuminate\Console\Command;
   
class FirstCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'first: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("First Cron is working fine!");
     
        /*
           Write your database logic we bellow:
           Item::create(['name'=>'hello new']);
        */
    }
}

Step 3: Setting Kernel File

Open app/Console/Kernel.php file and edit bellow code. 

<?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\FirstCron::class,
    ];
     
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('first:cron')->everyMinute();
    }
     
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
     
        require base_path('routes/console.php');
    }
}

Register as Task Scheduler

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

Step 4: Run Scheduler Command

Now the cron is ready to run, so you can check your cron's command manually. So let's run the command.

php artisan schedule:run

On successful execution of command you will receive the following output

Running scheduled command: '/usr/local/Cellar/php/7.4.2/bin/php' 'artisan' quote:daily > '/dev/null' 2>&1

After run above command, go to storage/logs/ and open laravel.php log file it looks like as bellow. 

storage/logs/laravel.php

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

Now Next, you can also automate the task scheduler for cron job.

crontab -e

Now, add the complete path in in crontab file of your application.

* * * * * 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 it can help you for laravel 8 cron job Tutorial...