Leveraging Laravel 5 Task Scheduler: Streamline Your Development Workflow Skip to main content

Task Scheduler

Saurabh Dhariwal

Saurabh Dhariwal

Laravel 5 task scheduler

Cronjob is a combination of two different entity. Cron is a time-based scheduler in Unix OS like Linux, FreeBSD, Mac OS etc... And these tasks are referred to as "Cron Jobs". So basic Cronjob is Unix command for scheduling jobs to be executed in specific time and specific interval OR we can say that cronjob is a some set of tasks that must be triggered at a certain interval.

 

For example we can schedule a task in the server that executes some scripts that sends out daily/weekly reports from our website. Mainly we are using the cron jobs to cleanup the databases, send the emails , execute time consuming tasks ….etc. So Here we need some task scheduling mechanism to do these periodic tasks.

 

Artisan command to manage the process of clearing out user events from the database that are at least one month old. Finally, we'll create a cron job to call this custom command every month.

 

Laravel 5.0 is introducing a pretty incredible cron-style scheduler (similar to Indatus' Dispatcher) built into the core. Just point a cron job that's scheduled once a minute at artisan schedule:run and you're good to go.

 

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

 

Here is an illustration with main five part. i.e. minute, hour, day of month, month & day of week

Laravel 5 task scheduler

This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.

Laravel’s task schedule is defined in the schedule method inside the app/Console/Kernel.php file. To schedule the commands we use the Laravel task scheduler. By using the command method we can add as many artisan commands we want. By using the schedule method we can schedule all the commands.

There are different schedule frequencies we can apply on the commands according to the need. Here is an example of a fictional task to clear the cache every day:

Laravel 5 task scheduler

The command method to schedule an Artisan command. We can go further and limit the execution of the task to a certain condition, using the when method which accepts a closure. The task will be executed only if the closure returns true.

There is available different scheduling methods are available with different schedule period, Please have a look for example.

Laravel 5 task scheduler

Example:

Let see example for schedule a task which send offer mail to existing user every minute. We need to manage the task execution definitions in app/Console/Kernel.php, which is presented below. Before that you also need to create command in 'app/Console/Commands/' as per below example. You can create this by Artisan commands. Use the make:console generator command as below:

$ php artisan make:console UpdateCatalog --command=send:offerMail

Laravel 5 task scheduler

Here, The $name and $description properties are defined the command’s execution name and description, respectively, both of which will be included in the Artisan list output once we register it within the app/Console/Kernel.php $commands array. You check more here.

Laravel 5 task scheduler

You can see that a send mail('send:offerMail') task has already defined in the scheduling method to run every minute interval.

Once you created, need to run the scheduler. And for that you'll need to add a single entry to your server's crontab file like below:

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

Here your code schedule: run Artisan command will run once per minute. It will, in turn, execute any jobs that you’ve defined using the Laravel command scheduler.

Feel free to contact us for any further issues and Laravel Development Services.

Frequently Asked Questions

What is Laravel 5 Task Scheduler, and why should I use it?

Laravel 5 Task Scheduler is a feature that allows you to automate repetitive tasks in your web application. It helps schedule jobs, like sending emails or clearing cache, so you don't have to do them manually.

How do I enable the Task Scheduler in Laravel 5?

Enabling the Task Scheduler is easy! You just need to set up a cron job on your server to run the schedule:run command. Laravel will handle the rest, executing scheduled tasks at specified intervals.

Can I schedule my custom tasks with Laravel 5 Task Scheduler?

Absolutely! Laravel 5 Task Scheduler lets you schedule built-in tasks and custom artisan commands. You can define the schedule and frequency according to your application's needs.

What kind of tasks can I schedule using Laravel 5 Task Scheduler?

You can schedule various tasks, such as sending emails, generating reports, updating database records, or any other command you want to run automatically at specific intervals.

How do I handle task failures in Laravel 5 Task Scheduler?

Laravel provides a simple way to handle task failures. You can define the maximum number of times a task can fail before it's considered unable to run. Additionally, you can set up notifications to alert you when a task fails.

Can I schedule tasks to run at specific times or intervals?

Yes, you have complete control over when tasks run. Laravel 5 Task Scheduler allows you to schedule tasks to run at specific times, on particular days, or at regular intervals, providing flexibility to suit your application's requirements.

Is Laravel 5 Task Scheduler suitable for shared hosting environments?

The ability to set up cron jobs may be limited in shared hosting environments. In such cases, you may need to check with your hosting provider or explore alternative solutions like external task scheduling services.