CronJob – Task Scheduling in Laravel

Every application requires some tasks to be run periodically on the server. It could be sending emails, creating reports, creating backups or cleanup databases. To automate these tasks Laravel offers cron jobs scheduling.
Cron is a time-based task scheduler in Unix/Linux operating systems. It runs shell commands at a pre-specified time period. Cron uses a configuration file called crontab also known as Cron table to manage the task scheduling process. Crontab contains all the Cron jobs related to a specific task. Cron jobs are composed of two parts, the Cron
expression, and a shell command that needs to be run

Laravel Cron Job Scheduling:
Laravel provides an easy way for task scheduling. Laravel’s ‘Command Scheduler’ allows you to easily define the schedule of the commands within Laravel itself. When using the scheduler, only one Cron entry is needed on the server. Your task schedule is defined in the app/Console/Kernel.php file’s schedule method.
Generate A New Command Class :
First, we will generate our own custom commands by running following commands in root directory which will generate a class in the app/Console/Commands/ directory.

This command will create a new command class Backup.php in the
app/Console/Commands directory. Navigate to the file and you will find the following code in it:

In this code, the following line contains the name and the signature of the command.
protected $signature = ‘command:name’;
Replace the words command:name with backup:daily. This is what we will call this when running the command to perform the task.
protected $signature = ‘backup:daily’;
protected $description = ‘Command description’;

This is where you have to place the description of what this command will actually achieve.
protected $description = ‘Daily backup of database’;
Finally, you now want to locate the handle() function that is called whenever you execute this command. This is where the logic or the code of your command should be present.
Registering the Command
Now that you have created the command, you will need to register it in the Kernel. Go to app/Console/Kernel.php file that looks like this

Change this file with the contents below. We have simply added our backup class to the commands array and schedule it to run daily in schedule function.

Now, if you run the php artisan list command in the terminal, you will see your command has been registered. You will be able to see the command name with the signature and description.

backup:daily Daily backup of database

You will see your custom command in the list. Now execute the command itself by php artisan backup:daily

Scheduling Artisan Commands
$schedule->command(backup:daily’) is where you have set which command needs to be executed and ->daily(); defines the time interval. In this example, I have set the time interval on a daily basis but there are many more frequencies that can be set as provided in the official Laravel documentation To set a different time period, all you will need to do is replace this ->daily(); with an option from the following list:
Schedule Frequency Options
There are a variety of schedules you may assign to your task:

Method Description
->cron(‘* * * * *’); Run the task on a custom Cron schedule
->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 minutes past the


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

Starting the Laravel Scheduler
Let’s setup the Cron Jobs to run automatically without initiating manually by running the command. To start the laravel scheduler itself, we only need to add one cron job which executes every minute. Go to your terminal, ssh into your server, cd into your project and run this command.
crontab -e
This will open the server Crontab file, paste the code below into the file, save and then exit.

  • * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Do not forget to replace /path/to/artisan with the full path to the artisan command of your laravel application. One of the most important advantages of laravel task scheduler is that we can focus on creating commands, writing logic and laravel will take care of the rest.