• Calculating...
  • 5 years ago
  • 7.3K Views
  • Archived This is an Archived post.
    The content within this may not be used or replaced with newer versions.

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

 
  1. 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!");
    }
}
 
  1. 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');
    }
}
 
  1. 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!   
 
 
  1. 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.

Share:

Related Post

CRUD Operations In Laravel 8

This tutorial is created to illustrate the basic CRUD (Create , Read, Update, Delete) operation using SQL with Laravel 8. Laravel is one of the fastest-growing frameworks for PHP.

  • 3 years ago

Connecting Multiple Databases in Laravel 5.8

This tutorial is created to implement multiple database connections using mysql. Let’s see how to configure multiple database connections in Laravel 5.8.

  • 5 years ago

Integrating Google ReCaptcha in Laravel 5.8

reCAPTCHA is a free service from Google. It’s a CAPTCHA-like system designed to recognize that the user is human and, at the same time, assist in the digitization of books. It helps to protects your w

  • 5 years ago

Clearing Route, View, Config Cache in Laravel 5.8

Sometimes you may face an issue that the changes to the Laravel Project may not update on the web. This occures when the application is served by the cache. In this tutorial, You’ll learn to Clear App

  • 5 years ago

Import, Export from Excel to Database in Laravel 5.8, 5.7, 5

This tutorial is created to Import, Export data from an excel sheet to database in Laravel Framework using maatwebsite/excel package. It provides an easy way to import and export using database model.

  • 5 years ago