Easy cron jobs in Laravel

Setting up cron jobs is easy in Laravel, since it has a build-in task scheduler. One of the main advantages of this task scheduler is that you only have to set one cron job in your DirectAdmin/cpanel environment. Once this is done, everything will be handled by Laravel. The cron job you have to set is:

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

The path to artisan is often something like /home/domainname/public_html/artisan

Create a command

In the next step, you have to write the code that has to be executed, which is done in a ‘command’. For example, we may want to send a reminder to the inactive users of our portal. A command can easily be created via the terminal:

php artisan make:command Reminder

A new file ‘Reminder.php’ is then created in the app/Console/Commands directory. Alternatively, you can create the command manually, but make sure that your class extends the ‘Command’ class.

Within this Reminder class, you find two important parts: the protected variable $signature that is used to schedule the task, and the function ‘handle’ in which you write the code to be executed. For example, in the handle function you retrieve all users that have been inactive for x weeks and send them an email.

Kernel

In the app/Console/Kernel.php file you can schedule the task. First, you add you command to the protected $commands array. Next, in the schedule function, you schedule your task:

protected $commands = [
    'App\Console\Commands\Reminder'
];

protected function schedule (Schedule $schedule) {
    $schedule->command('reminder')->dailyAt('13:00')->sendOutputTo('storage/cronlog')->emailOutputTo('info@plint-sites.nl');
}

Make sure that the name of the command, ‘reminder’ in this case, corresponds to the signature you used in the command.

There is plenty of frequency options for the cron job, e.g. hourly, weekly, everyFiveMinutes etc. A full overview of the possibilities can be found here.

Additionally, the output of the cron job can be stored on the server using the ‘sendOutputTo’ method, or send by email using the ‘emailOutputTo’ method. As you can see it is perfectly fine to use both.

Testing cron jobs on a localhost

Testing cron jobs can be quite tricky and time consuming. Additionally, on a localhost, it’s not always possible to set a cron job, but using the task scheduler it becomes easy. First, you change the frequency to ->cron(‘* * * * * *’) to let the cron job run every minute. Next, you initialize the cron job via the command line with

php artisan schedule:run

Now, the cron job will be executed once and you are able to test and debug the script. When you’re done testing, don’t forget to change back the cron job frequency!

Update:

The examples in this blog are updated to Laravel 5.3 at 7 december 2016.


Mijn Twitter profiel Mijn Facebook profiel
Leonie Derendorp Webdeveloper and co-owner of PLint-sites in Sittard, The Netherlands. I love to create complex webapplications. I am always busy to learn new backend techniques and I stay on top of ongoing development. Websites and webapplications should be intuitive to use and that is what we take care off. Latest post
Transition to Laravel Forge and Envoyer - part 1
This entry was posted in PHP and tagged on by .

About Leonie Derendorp

Webdeveloper and co-owner of PLint-sites in Sittard, The Netherlands. I love to create complex webapplications. I am always busy to learn new backend techniques and I stay on top of ongoing development. Websites and webapplications should be intuitive to use and that is what we take care off.

2 thoughts on “Easy cron jobs in Laravel

Leave a Reply

Your email address will not be published. Required fields are marked *