Laravel 5: Scheduler – Set up Cron on Windows

Scheduler: Set up Cron on Windows


WINDOWS 10 UPDATE

In Windows 10, you need to make sure that task runs in Tab::General-> Security Options, under SYSTEM user, otherwise you will see CMD console popping up.
But before you go for it, you may read this StackOverlow post.
If you do not like what you read there, you may try to go for Z-Cron free soft.


Laravel docs explain how to setup Cron on Linux based machines.
If you develop on Windows based machine, e.g. on XAMPP as I do, then you are in a fix.
No worries, below you will find solution.

As you probably know, there is a difference between Windows and Linux/Unix (Mac).

Linux system cron

Just add this code:

There are plenty tutorials online.

Windows has GUI equivalent called Task Scheduler, that can be made to perform similar function.

 

Before I go into this, let me tell you, that you can run schedules by hand, by using this command in Command Prompt:

Remember to navigate to project directory first.

 

Now, let me tell you, how to set it up on Windows.

It is not that hard.

First task is to create some schedule to run and some dependencies: testing directory and file.

Go to this file:

… and uncomment and slightly append (for the sake of this example) this:

Some portion of that method above is already there, just commented out. Some stuff needs to be added.

Chained method: appendOutputTo has a path inside.
I just created a testing directory inside of main project directory, but you can place it anywhere.
For coding purposes it is enough.

Inside of that directory, I placed file: test.txt.
And that way we got this path:

 

Now, we can setup our Windows based Cron.
Open ‘computer management’ window.

ts1

 

Select ‘Task Scheduler’

ts2

 

In right-hand ‘Actions’ panel, select ‘Create Task’

ts3

 

In open panel, make selections as shown.

ts4

  1. Name your task, so you can find it on a task list – can be anything
  2. Description – anything you like here
  3. Make sure you have all 3 ticked
  4. Select your OS.
  5. Now, go to TAB: Triggers and click on button ‘New’ (bottom left)

 

In panel ‘New Trigger’ select as shown on image below:

ts5

Just to recap:

  1. select ‘Daily’ – every 1 days
  2. select ‘Repeat task every:’ and enter by hand “1 minute” (to emulate Laravel Cron setting)
  3. Make sure ‘Enabled’ is ticked
  4. Click OK button

 

Go to TAB: Actions, and click ‘New’ button

ts6

  1. This is directory, where you have php.exe file powering PHP in your localhosted server.
    I use XAMPP, and that is, where my php.exe is located.
    Yours may be elsewhere.
  2. Here you should enter directory to your ‘artisan’ file, which is located in main directory of your project, in my case it is: U:\www\la4\artisan
    Additionally, it should have command to fire: schedule:run
    In total, what you need to enter in a field pointed by #2 arrow looks like this:

Click button ‘OK’, and again button ‘OK’.

You are done. Your WinCron should run now every minute, emulating Laravel one.

NOTE:
When you are done testing:
  • go to “New Trigger” panel and set: “Stop task, if it runs longer than:”  to some expiry setting, of your choice, or
  • delete this task from Task Scheduler, or
  • disable task by hand at the end of testing, if you want to keep it for later testing

Of course above note is optional.