If you are running a Laravel application on a Windows machine, you may need to schedule tasks to run at specific times or intervals, such as sending out emails, cleaning up databases, or processing queued jobs. In Unix-like systems, this is typically done using cron jobs. However, on Windows, we use Task Scheduler to achieve the same functionality.
In this post, we’ll show you how to set up a cron job in Windows OS for your Laravel application using Task Scheduler.
What is a Cron Job?
A cron job is a scheduled task that runs automatically at a predefined time. In Laravel, you can schedule tasks like database cleanup, sending scheduled emails, or running artisan commands at specific intervals. Laravel provides an easy way to set up these tasks using the schedule:run
Artisan command, which can be scheduled using a cron job in Linux or a Task Scheduler in Windows.
Steps to Set Up a Cron Job in Windows for Laravel
1. Open Task Scheduler
-
Press the Windows key, type Task Scheduler, and open it.
-
In Task Scheduler, we’ll be using the Create Task option to set up a more customized and detailed schedule compared to the Create Basic Task option.
2. Create a New Task
-
In the Actions panel on the right, click Create Task.
-
Give your task a meaningful name, such as Laravel Scheduler.
-
Optionally, you can provide a description to remind you what this task does.
3. Configure General Settings
In the General tab:
-
Security options: Choose “Run whether user is logged on or not” to ensure that the task will run even if you're not logged into Windows.
-
Run with highest privileges: Check this box to give the task administrative rights, which might be necessary for certain Laravel operations like file manipulation or database access.
4. Set the Trigger
Now, we’ll set up the schedule for the task.
-
Go to the Triggers tab and click New.
-
In the New Trigger window:
-
Set the Begin the task dropdown to On a schedule.
-
Select Daily, and set the task to repeat every 1 day (you can adjust this depending on your needs).
-
Set the Start time to the desired time to begin the task (typically, you would want this to be after the server has started or a few minutes after).
-
Check the box that says Repeat task every and set it to 1 minute, which is important for Laravel’s scheduler to run at intervals.
-
Ensure that for a duration of is set to Indefinitely so the task keeps running.
-
Click OK once you're done with the trigger settings.
5. Set the Action
Next, we’ll specify what the task will do when triggered.
-
Go to the Actions tab and click New.
-
Set the Action to Start a program.
-
In the Program/script field, browse to your PHP executable file. For example, if you’re using XAMPP, it might be located in
C:\xampp\php\php.exe
. If you’ve installed PHP separately, it will be wherever your PHP executable is located. -
In the Add arguments (optional) field, enter the following command:
artisan schedule:run
This command tells Laravel to run its scheduled tasks.
-
In the Start in (optional) field, enter the path to your Laravel project directory. For example, if your Laravel project is located in
C:\laravel\myapp
, you’ll want to enter that path.
Click OK to confirm the action.
6. Set Conditions (Optional)
You can configure the Conditions tab to make the task run only if certain conditions are met. For example, you might want to only run the task if the computer is idle or if it’s on AC power.
If you don't need any specific conditions, you can skip this step and proceed.
7. Set Settings
In the Settings tab:
-
Make sure to check Allow task to be run on demand.
-
Check If the task fails, restart every and set it to 1 minute to ensure the task keeps running even if something goes wrong.
-
Optionally, you can set the task to stop after a certain duration, but for most Laravel jobs, leaving this unchecked works fine.
Click OK to finish setting the task.
8. Test the Cron Job
Now that you’ve set up the cron job, it’s important to test it to ensure everything works properly:
-
In Task Scheduler, right-click your newly created task and select Run to test if it runs without any errors.
-
You can check your Laravel logs (located in
storage/logs
) to verify that the scheduled tasks are being executed as expected.
Conclusion
That’s it! You've successfully set up a cron job for your Laravel application on Windows using Task Scheduler. With this setup, you can run Laravel’s scheduler commands automatically at regular intervals without needing to be logged in or manually triggering them.
Setting up cron jobs on Windows might seem tricky at first, but once you've followed these steps, you'll have a seamless, automated process running in the background to handle your scheduled tasks. If you run into any issues, be sure to check your paths and permissions, and always test your tasks thoroughly.