You are currently viewing Laravel 5 Cron Job Scheduler
vteams- blog

Laravel 5 Cron Job Scheduler

  • Post published:October 23, 2015

Laravel 5 – Scheduler made scheduling tasks very easy. One must know that why the need of Scheduler arose? Previously, cron jobs were used for scheduling tasks.

Cron Job:

Cron is basically a Linux utility which schedules a command/script to run automatically on server at a specified time and date. A cron job is very useful to automate a repetitive task as it is a scheduled task itself. For instance, lets say if one need to set a cron job to delete temporary files every week, as a result the disk space will be conserved.

Problem with adding a cron job was that one have to schedule each and every cron on server and then have to repeat the same steps several times. To overcome this problem, Laravel provided a new feature as “Laravel 5 – Scheduler”.

Scheduler:

This new feature was released in Laravel 5 with the name of “Artisan scheduler”. It is designed by keeping in mind the purpose of simplifying tasks that need to be scheduled. One cron job is required to set up and you need to run it every minute by the following command:

php artisan schedule:run

As soon as it is setup, one can schedule any task to run. Cron scheduling is not very hard to learn as it is really simple. Every schedule is created inside “app/Console//Kernel.php” file. For example, to clear the cache every hour one can call for the following:

i) Use Class methods:
$schedule->call('SomeClass@method')->dailyAt('10:00');
ii) Use Closure:
$schedule->call(function(){
    //..
})->everyThirtyMinutes();
iii) Use Terminal Commands:
$schedule->terminal('gulp task')->fridays()->when(function(){
     return true; 
});

The best thing about Laravel is that it always keep developers in mind while formulating new features. Different scheduling methods are available as follows:

->hourly()
->daily()
->at($time) // 24 hour time
->dailyAt($time)
->twiceDaily()
->weekdays()
->mondays()
->tuesdays()
->wednesdays()
->thursdays()
->fridays()
->saturdays()
->sundays()
->weekly()
->weeklyOn($day, $time)
->monthly()
->yearly()
->everyFiveMinutes()
->everyTenMinutes()
->everyThirtyMinutes()
->days() // Days of the week