Laravel Task Scheduling with Cron Job Tutorial
Laravel’s command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. When using the scheduler, only a single Cron entry is needed on our server.
In this tutorial, we are going to do a 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
# or,
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
The tutorial is over. Thank you.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.