Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save haakym/e97808c906441ca4344457725cd29a8b to your computer and use it in GitHub Desktop.
Save haakym/e97808c906441ca4344457725cd29a8b to your computer and use it in GitHub Desktop.
Automating the Laravel Task Scheduler on Windows from stillat.com

Automating the Laravel Task Scheduler on Windows

In this post we will look at how to automate the Laravel Task Scheduling system 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:

imgWindows Services

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:

imgWindows Task Scheduler

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:

imgCreate 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:

imgCre

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:

imgwin_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:

imgwin_new_trigger_dialog_filled

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 it’s 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:

imgwin_new_trigger_modifications

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 it’s 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:

imgwin_create_task_settings

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:

imgwin_new_action

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:

"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):

imgwin_new_action_complete

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.

imgwin_enable_task

Conclusion

While automating the Laravel Task Scheduler on Windows is not as easy as adding a cron entry, it is not overly complicated; it just involves a few steps. Found this useful? Here are a couple things you can do to support my work:

Follow me on Twitter @StillatOffical and share this post with your friends and co-workers (sharing with pets is fine too, if they are in to this sort of stuff) Just take a moment to browse the site. There’s a search field on the left where you can find even more related content! This article is part of a book I am working on. If you liked this article, consider purchasing a copy to support all the free articles and continued book updates! Find it here: laravelartisan.com

Source: https://web.archive.org/web/20160810080642/http://www.stillat.com:80/blog/2016/08/02/automating-the-laravel-task-scheduler-on-windows/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment