« Task Scheduling (CRON) in Laravel 5

August 6, 2019 • ☕️ 2 min read

Laravel

asset 1


In the past, adding Cron entries required SSHing into your server to add each entry or task. Laravel’s task scheduling feature gives you the full power of CRON in a fluent API without having to SSH into your server to add additional Cron entries. It allows you to fluently and expressively define your task schedule within Laravel itself.

Personally, I never really understood CRON and felt it might be too difficult to set up without CPanel or on a VPS Server but after trying out Laravel’s Task Scheduling feature, I was blown away by its simplicity. In this article, I set out to define (to the best of my knowledge) what CRON is and how you can take advantage of it in your Laravel application.


What is CRON?

CRON is a scheduling daemon that executes tasks at specified intervals. These tasks are called CRON Jobs and are mostly used to automate system maintenance or administration. You can schedule CRON Jobs to run by the minute, hour, day of the month, month, day of the week or any combination of these.

For example, you could set a CRON job to backup your databases or data, update your system with the latest security patches, check your disk space usage, send emails and more. A more practical example will be a large SaaS application that selects 10 random customers every hour during the weekend and offers them a discount. Creating a job or a script for sending the discount can be pretty easy but we need a way to run it every hour and only on weekends. That’s where CRON comes in handy.

You communicate with this scheduling daemon using crontab files. Crontab (CRON table) is a text file that specifies the schedule of CRON Jobs. Although you can edit the user crontab files manually, it is recommended to use the crontab command.

If you are a geek like me and want to know more about CRON Jobs such as the cronjob syntax and many more, read more here. You can use a tool like crontab.guru for advanced use-cases and a better understanding of how your scheduled tasks are going to run.


Setup CRON Jobs on your Server

When using the inbuilt Laravel task scheduler, you only need to add the following Cron entry to your server;

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

To add the CRON entry, run the command below in your terminal to edit your crontab file.

1crontab -e

Add the above CRON entry to the end of the file. The above CRON entry will inform the CRON daemon to run the artisan command below every minute.

1php artisan schedule:run

Pretty easy right? You only have to register one CRON job in your crontab and Laravel takes care of the rest under the hood.


Defining a Scheduled Task

To define your scheduled commands, add them inside the schedule method of Console kernel located at App\\Console\\Kernel.php. To try it out, add the code below to your schedule method;

1// App\Console\Kernel.php
2
3<?php
4
5 /*
6 * Define the application's command schedule.
7 *
8 * @param \Illuminate\Console\Scheduling\Schedule $schedule
9 * @return void
10 */
11 protected function schedule(Schedule $schedule)
12 {
13 $schedule->call(function () {
14 Mail::send('emails.send',['title'=> 'My App Scheduler', 'content' => 'My First App Scheduler is working'], function ($message) {
15 $message->from('notify@myapp.com', 'My App');
16 $message->to('baffouraduboampong@gmail.com');
17 });
18 })->everyFiveMinutes();
19 }
20?>

NB: The above code assumes you have added the Mail Facade(use Illuminate\Support\Facades\Mail), have an email view template in your emails folder inside your views called send.blade.php and have mail services already configured in your application.

Voila! You should receive an email from your application every 5 minutes. To know more about the scheduling methods Laravel provides, head to the official documentation