Laravel Task Scheduling: Running the Task Scheduler

December 7, 2016 —John Koster

In this article we will explore the various ways to run the Laravel task scheduler. We will look at running the task scheduler on Mac OS as well as on Windows.

There are two major ways to run the task scheduler. The first is to manually run the scheduler using the schedule:run Artisan command:

1php artisan schedule:run

However, the scheduler is most useful when it is automatically called. On systems that support Cron, the following entry can be added to automatically run the task scheduler every minute. Laravel will handle the scheduled tasks and run any that are due automatically:

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

It is important to update the /path/to/artisan part of the Cron entry to match the path of the Artisan file for the particular application you want the task scheduler to run on.

Starting the task scheduler is a little different on Windows systems, but is possible using the Windows Task Scheduler.

#Adding a crontab Entry

Adding an entry to crontab on Unix-like systems where crontab is available is fairly straightforward. We can use the crontab -e command to edit the crontab entries. The command may request that you specify the editor to use when adding or modifying crontab entries.

Entering the following command:

1crontab -e

Would produce results similar to this on a system that does not have any entries. The following is the default heading for the crontab configuration file, if no crontab configuration file exists yet a new one will be generated.

1# Edit this file to introduce tasks to be run by cron.
2#
3# Each task to run has to be defined through a single line
4# indicating with different fields when the task will be run
5# and what command to run for the task
6#
7# To define the time you can provide concrete values for
8# minute (m), hour (h), day of month (dom), month (mon),
9# and day of week (dow) or use '*' in these fields (for 'any').#
10# Notice that tasks will be started based on the cron's system
11# daemon's notion of time and timezones.
12#
13# Output of the crontab jobs (including errors) is sent through
14# email to the user the crontab file belongs to (unless redirected).
15#
16# For example, you can run a backup of all your user accounts
17# at 5 a.m every week with:
18# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
19#
20# For more information see the manual pages of crontab(5) and cron(8)
21#
22# m h dom mon dow command

Using the editor of your choice, you must add the following entry into the crontab configuration file; be sure to replace the /path/to/artisan section with the full path the application's artisan file:

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

Once this entry has been added, follow the instructions in your editor of choice to save the file. The crontab -l command can be issued to verify that the entry was successfully installed.

#Configuring the Windows Task Scheduler

The following sections will explain how to automate the Laravel Task Scheduler using the Windows Task Scheduler. The instructions presented were written for the Windows 10 Professional operating system, but should be easily transferable to other Windows operating system versions. The Windows Task Scheduler service must first be enabled and running on the system. To verify this, open the Services application from the Administrative Tools panel on the specific Windows system you are using (alternatively you can run the services.msc application from the Run Window by using the Windows Key + R key combination). Locate the "Task Scheduler" entry and verify that it is running:

Running the Task Scheduler | Verify that the Task Scheduler service is running.

Now, start the Task Scheduler management application by selecting the "Task Scheduler" application from the Administrative Tools panel (alternatively you can run the taskschd.msc application from the Run Window by using the Windows Key + R key combination). The Task Scheduler management application should appear on the screen:

Running the Task Scheduler | Start the Task Scheduler management application.

To create a new task to automatically run the Laravel Task Scheduling system we first need to open the Create Task dialog. Do this by selecting the "Create Task..." option from the options panel on the right or by selecting "Action > Create Task..." from the application menu. The following dialog will appear:

Running the Task Scheduler | Opening the Create Task dialog.

Start by supplying a Name and a Description for the new task. The task in this section will use the following values:

Name

: Laravel Task Scheduler

Description

: Runs the Laravel Task Scheduler system automatically every minute.

While on this screen, select the "Run whether use is logged on or not" if it applicable to your use case:

Running the Task Scheduler | Setting appropriate details for the new task.

Now, activate the Triggers tab located at the top of the Create Task dialog, directly to the right of the General tab. Once the Triggers tab has loaded click the "New..." button located in the bottom left of the Triggers tab pane. The New Trigger dialog will appear:

Running the Task Scheduler | Opening the New Trigger dialog.

Ideally, we want the Laravel Task Scheduler to start automatically when the system starts. This can be accomplished by selecting the value "At startup" for the "Begin the task" setting. On the same dialog, under the "Advanced settings" control group, check the box next to "Repeat task every". This will enable the controls required to frequency the Laravel Task Scheduler should run, and for how long. By default, new tasks are set to repeat every 1 hour. Replace this default value with 1 minutes (the New Trigger dialog will allow you enter arbitrary intervals manually). After this, set the duration to Indefinitely. The New Trigger dialog should now look similar to the following figure:

Running the Task Scheduler | Setting the appropriate settings for the new trigger.

Click the "OK" button to add the new trigger to the task. Currently, the task will only start when the Windows system has started. This means that the task will not start running after the its creation, or when modifications have been made to the task without restarting the system. This can be remedied by creating another trigger. Click the "New..." button again to create a new trigger. For the "Begin the task" option, select the value "At task creation/modification". Apply the same settings in the "Advanced settings" control group for this new trigger that were applied when creating the "At startup" trigger:

Running the Task Scheduler | Create a new trigger to run at creation or on trigger modification.

After the new triggers have been entered and have appeared in the triggers list, click the Settings tab (the last tab option at the top of the Create Task dialog window). On this tab, check the box next to the "Run task as soon as possible after a schedule start is missed". If the Laravel Task Scheduler should stop running after a certain time period, specify this next to the "Stop the task if it runs longer than" option; if the Artisan Task Scheduling system should be allowed to run until its tasks have completed, simply un-check the "Stop the task if it runs longer than" option. At the bottom of the tab content, locate the "If the task is already running, then the following rule applies:" option and then select the value "Run a new instance in parallel" (the Laravel Task Scheduler provides mechanisms to prevent concurrent task execution). The Settings tab should look similar to the following figure:

Running the Task Scheduler | Applying the settings to the new task.

At this point, we have all the settings required to execute the Laravel Task Scheduler. However, we have not specified the actual task that the Windows Task Scheduler should run. To have the Windows Task Scheduler run the Laravel Task Scheduling system, click the Actions tab at the top of the Create Task dialog window. When the tab content appears, click the "New..." button in the lower left corner of the tab contents to display the New Action dialog window:

Running the Task Scheduler | Opening the New Action dialog window.

It is by using the options found on this window that we can instruct the Windows Task Scheduler to execute the Laravel Task Scheduler. Ensure that the "Action" option is set to the "Start a program" value (it should be set to this by default). Within the Settings control group, set the "Program/script" value to php (if PHP has been set in the PATH environment variable). If the path to the PHP executable has not been set in the PATH environment variable, the full path to the PHP executable must be supplied as the value for "Program/script".

Since the Windows Task Scheduler will be calling the PHP executable every minute, we need to supply arguments to PHP that instructs it on what script to run. This can be done using the "Add arguments (optional)" option. The arguments we need to supply are the full path to our applications artisan script as well as the Artisan command we want to run (in this case the Artisan command we want to run every minute is schedule:run). The following option format should be used, including the quotes, when supplying the value for the "Add arguments (optional)" option:

1"path\to\artisan" schedule:run

The New Action dialog should look similar to the following (make sure that a valid path for the Artisan script and the schedule:run command have been supplied for the "Add arguments (optional)" option):

Running the Task Scheduler | Setting the program/script that will be ran by the task.

Once all the appropriate fields have been completed, click the "OK" button to add the new action to be returned to the Create Task dialog window. On the Create Task dialog window, click the "OK" button to save the new task. Locate the new "Laravel Task Scheduler" task within the Task Scheduler Library. If it is disabled, right-click the task and click the "Enable" option. If the task has not ran yet, it can be manually ran by right clicking the task (after it has been enabled) and then selecting the "Run" option, or by selecting "Action > Run" from the application menu.

Running the Task Scheduler | Ensure that the new task is enabled.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.