Scheduling Tasks with Cron Job in Laravel 5.8

Cron Job is used to schedule tasks that will be executed every so often. Crontab is a file that contains a list of scripts, By editing the Crontab, You can run the scripts periodically. Cron is a task scheduler daemon which is used to run scheduled tasks at specific intervals. Cron uses the configuration file called Crontab, also known as cron table, to manage the scheduling process.

# Laravel 5.8

Laravel 5.8 continues the improvements made in the previous release (version 5.7). Refer release notes to see the changes made in Laravel 5.8.

In this tutorial, You'll learn to implement Cron Jobs to schedule tasks in Laravel 5.8. Follow the step by step guide to create the Cron Job.

  1. Install Laravel 5.8

    First, let install Laravel 5.8 using the following command (Make sure you have installed composer in your PC). Click on Install Composer If you haven't installed Composer on your PC.

    composer create-project --prefer-dist laravel/laravel dummyCron
  2. Create the Custom Command

    In this step, You need to create the custom command class by typing the following command in your terminal.

    php artisan make:command DummyCron --command=dummy:cron

    The cron job will be created under "app/Console/Commands/". Edit the file with the code below to implement the logic.

    app/Console/Commands/DemoCron.php

    <?php
    namespace App\Console\Commands;
    use Illuminate\Console\Command;
    
    class DummyCron extends Command
    {
        protected $signature = 'dummy:cron';
        protected $description = 'Dummy Cron Job Application';
    
        public function __construct()
        {
            parent::__construct();
        }
    
        public function handle()
        {
            \Log::info("Running!");
        }
    }
  3. Register the Custom Command

    After successfully creating DemoCron.php file, we need to register this command in kernel.php file with task scheduling as seen below,

    $schedule->command('dummy:cron') is where we define what command needs to be executed and ->everyMinute(); defines the frequency of execution. There are more time intervals that we can define. You can find more about Task Scheduling in Laravel Documentation. Some of the Schedule Frequency Options as below,

    Method Description
    ->everyMinute(); Run the task every minute
    ->everyFiveMinutes(); Run the task every five minutes
    ->hourly(); Run the task every hour
    ->daily(); Run the task every day at midnight
    ->weekly(); Run the task every week

    app/Console/Kernel.php

    <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        protected $commands = [
            Commands\DummyCron::class,
        ];
    
        protected function schedule(Schedule $schedule)
        {
            $schedule->command('dummy:cron')->everyMinute();
        }
    
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
            require base_path('routes/console.php');
        }
    }
  4. Run Scheduler Command on Localhost

    In this step, Run the below command to test the CRON Job at your localhost.

    php artisan schedule:run

    After running the above command, You'll get the output in your Log file as below.

    [1994-08-01 01:10:42] local.INFO: Running!
    [1994-08-01 01:10:43] local.INFO: Running!
    [1994-08-01 01:10:44] local.INFO: Running!  
  5. Run Scheduler Command on Live Server

    Type the following command at your project directory to run Cron Job at your Live Server

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

Hope this tutorial helped you! Feel free to drop your opinion at the comment section.