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.


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 = [

protected function schedule (Schedule $schedule) {

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!


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 using Laravel! Latest post
A scalable server setup using Laravel Forge and Envoyer

4 thoughts on “Easy cron jobs in Laravel

  1. saman

    hi . I did all you have told in this article but i received periodic emails that say ‘Class ‘Illuminate\Console\Command’ not found ‘. i confuse because in cron.php (the file i created by artisan:consol ),
    there are “namespace App\Console\Commands;

    use Illuminate\Console\Command;”
    and cron class extends Command
    where i have made a mistake?
    best regards

    1. Leonie Derendorp Post author

      Hi Saman,

      Your code seems fine, but it’s hard to tell what’s going wrong from these few lines of code.

      The Illuminate\Console\Command class apparently cannot be found, so maybe it helps to run a composer dump-autoload to regenerate the class map.


Leave a Reply

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