Task Scheduling with Cron Job in Laravel
In this tutorial, we are going to do task scheduling cronjob in Laravel.
Table of Contents
Step 1 : Create a Command
At first, we need to create a custom command that will execute with task scheduling cron job. Let’s create this by typing this artisan command:
php artisan make:command CronTest
Go to app>Console>Commands and open CronTest.php file. We have to set $signature and the scheduled task will be in handle() method.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CronTest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test: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()
{
/*
Write your database logic here:
*/
\Log::info("Cron is working.");
}
}
Step 2 : Register Command on Task Scheduler
Open app>Console>Kernel.php and on the schedule() function, let’s register our custom command. We need our custom command’s signature. Here’s the example:
<?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\CronTest::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('test:cron')->everyMinute();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__ . '/Commands');
require base_path('routes/console.php');
}
}
Here, everyMinute() will run the task every minute. You can get more schedule frequencies from Laravel Task Scheduling page.
Step 3 : Run the Scheduler
We are ready to run the cron job. Let’s run by entering this command:
php artisan schedule:run
Now go to storage/logs folder and you will find a file like “laravel-2019-05-30.log“. Open the file and you can see like:
[2019-05-30 16:46:29] local.INFO: Cron is working.
Step 4 : Register Cron on Server
We have checked that our cron job is working. After publishing the project, you need to register the cron job in the server’s crontab file.
Here’s the example of registering cron job in the server:
* * * * * cd /your-project-path && php artisan schedule:run >> /dev/null 2>&1
The tutorial is over. Thank you. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)