Skip to main content

How to run cron jobs on Windows

·8 mins

Elephant holding a clock
Elephant holding a clock
Photo generated by DALL-E

Laragon 6 added the ability to run cron jobs. Here is an example on how to run them on Windows using Cronical.

Documentation #

The official documentation for Cronical is in the README on GitHub mgefvert/Cronical.

There is also more documentation and examples in the cronical.dat file (Laragon Menu > Tools > cron > cronical.dat).

Installation #

With Laragon #

You don’t need to install Laragon to use Cronical. I would recommend Laragon for WAMP/WEMP development. I’ve been using it for years, I’ve found it the best development environment. Cronical is included with Laragon 6.

Manual installation #

The Cronical.exe is a stand-alone package, the only requirement is .NET 4.7.2. The code can be downloaded or cloned from the repo. The Cronical.exe is in the Binaries directory. Once downloaded or cloned follow the documentation. Amend the cronical.dat, install, and run the Cronical.exe as required.

Example #

In this example I will use two of the existing examples from conical.dat and modify them to echo text to a log.

Open conical.dat (Menu > Tools > cron > cronical.dat)

Copy the example for every minute and every 5 minutes to the end of the file and remove the comment #

Add >> my_log.txt to the end of each so the echo is piped to a log file my_log.txt

# --- Jobs --------------------------------------------------------

*    *    * * *        cmd /c echo Every minute >> my_log.txt

*/5  *    * * *        cmd /c echo Every five minutes >> my_log.txt

Start Terminal (or CMD or PowerShell) in admin mode by holding down the Windows key Win press x. Select Terminal (Admin) from the menu.

When prompted Allow elevated privileges.

In Terminal navigate to cronical in laragon. E.g. by default:

cd c:\laragon\bin\cronical

The easiest way to see cronical working, is by running it in debug mode:

.\Cronical.exe --install
.\Cronical.exe --console --debug

For more options see the official documentation.

The above command will start Cronical. The –debug switch will output the process to the terminal. Leave it running for five minutes and both jobs will have run (…. = truncated output for brevity):

20:02:31.213 ***   Cronical booting up
20:02:31.223       Starting Cronical as a console program...
20:02:31.224 ***   Process startup
20:02:31.224       Using definition file cronical.dat
20:02:31.235       - Next job start 12/01/2023 20:03:00 for cmd /c echo Every minute >> my_log.txt
20:02:31.237       - Next job start 12/01/2023 20:05:00 for cmd /c echo Every five minutes >> my_log.txt
20:02:31.238       Config: Run missed jobs on startup = True
20:02:31.238       Config: Check services every       = 15 seconds
20:02:31.239       Config: Terminate cron jobs after  = 3600 seconds
20:02:31.239       2 jobs in job list
20:02:31.240       - Starting boot jobs
20:02:31.241       - Run missed jobs mode - recalculating jobs execution time from last activity...
20:02:31.241       - Next job start 12/01/2023 19:56:00 for cmd /c echo Every minute >> my_log.txt
20:02:31.241       - Next job start 12/01/2023 20:00:00 for cmd /c echo Every five minutes >> my_log.txt
20:02:32.247       - [4] Next job start 12/01/2023 20:03:00 for cmd /c echo Every minute >> my_log.txt
20:02:32.248       - [4] Next job start 12/01/2023 20:05:00 for cmd /c echo Every five minutes >> my_log.txt
20:02:32.248       - [4] Checking services
20:02:32.249       [5] Starting job: cmd /c echo Every minute >> my_log.txt
20:02:32.249       [6] Starting job: cmd /c echo Every five minutes >> my_log.txt
20:02:32.249       [4] Hello! I have 2 upcoming jobs today and 0 services running.
20:02:32.250       - [5] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:02:32.250       - [6] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:02:32.251       - [5] ...parameters: /c echo Every minute >> my_log.txt
20:02:32.251       - [6] ...parameters: /c echo Every five minutes >> my_log.txt
20:02:32.251       - [6] ...in directory: C:\laragon\bin\cronical
20:02:32.251       - [5] ...in directory: C:\laragon\bin\cronical
20:02:32.259       - [6] Process started, waiting at most 3600 seconds
20:02:32.259       - [5] Process started, waiting at most 3600 seconds
20:02:33.282       - [6] Job finished
20:02:33.282       - [5] Job finished
20:02:47.382       - [7] Checking services
20:03:00.495       - [8] Next job start 12/01/2023 20:04:00 for cmd /c echo Every minute >> my_log.txt
20:03:00.495       [9] Starting job: cmd /c echo Every minute >> my_log.txt
20:03:00.496       - [9] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:03:00.496       - [9] ...parameters: /c echo Every minute >> my_log.txt
20:03:00.497       - [9] ...in directory: C:\laragon\bin\cronical
20:03:00.500       - [9] Process started, waiting at most 3600 seconds
20:03:01.525       - [9] Job finished
20:03:02.520       - [7] Checking services
...
20:04:01.069       - [8] Next job start 12/01/2023 20:05:00 for cmd /c echo Every minute >> my_log.txt
20:04:01.069       [10] Starting job: cmd /c echo Every minute >> my_log.txt
20:04:01.069       - [10] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:04:01.069       - [10] ...parameters: /c echo Every minute >> my_log.txt
20:04:01.069       - [10] ...in directory: C:\laragon\bin\cronical
20:04:01.074       - [10] Process started, waiting at most 3600 seconds
20:04:02.092       - [10] Job finished
20:04:03.091       - [4] Checking services
....
20:05:00.639       - [4] Next job start 12/01/2023 20:06:00 for cmd /c echo Every minute >> my_log.txt
20:05:00.639       - [4] Next job start 12/01/2023 20:10:00 for cmd /c echo Every five minutes >> my_log.txt
20:05:00.639       [11] Starting job: cmd /c echo Every minute >> my_log.txt
20:05:00.640       - [11] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:05:00.640       [12] Starting job: cmd /c echo Every five minutes >> my_log.txt
20:05:00.641       - [12] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:05:00.641       - [12] ...parameters: /c echo Every five minutes >> my_log.txt
20:05:00.642       - [12] ...in directory: C:\laragon\bin\cronical
20:05:00.640       - [11] ...parameters: /c echo Every minute >> my_log.txt
20:05:00.642       - [11] ...in directory: C:\laragon\bin\cronical
20:05:00.645       - [12] Process started, waiting at most 3600 seconds
20:05:00.648       - [11] Process started, waiting at most 3600 seconds
20:05:01.678       - [11] Job finished
20:05:01.678       - [12] Job finished
20:05:03.670       - [4] Checking services
....
20:06:01.242       - [4] Next job start 12/01/2023 20:07:00 for cmd /c echo Every minute >> my_log.txt
20:06:01.243       [10] Starting job: cmd /c echo Every minute >> my_log.txt
20:06:01.243       - [10] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:06:01.243       - [10] ...parameters: /c echo Every minute >> my_log.txt
20:06:01.244       - [10] ...in directory: C:\laragon\bin\cronical
20:06:01.247       - [10] Process started, waiting at most 3600 seconds
20:06:02.268       - [10] Job finished
20:06:04.272       - [8] Checking services
....
20:07:00.774       - [7] Next job start 12/01/2023 20:08:00 for cmd /c echo Every minute >> my_log.txt
20:07:00.775       [9] Starting job: cmd /c echo Every minute >> my_log.txt
20:07:00.775       - [9] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:07:00.775       - [9] ...parameters: /c echo Every minute >> my_log.txt
20:07:00.775       - [9] ...in directory: C:\laragon\bin\cronical
20:07:00.779       - [9] Process started, waiting at most 3600 seconds
20:07:01.802       - [9] Job finished
20:07:04.818       - [7] Checking services
....
20:08:01.369       - [8] Next job start 12/01/2023 20:09:00 for cmd /c echo Every minute >> my_log.txt
20:08:01.370       [6] Starting job: cmd /c echo Every minute >> my_log.txt
20:08:01.370       - [6] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:08:01.371       - [6] ...parameters: /c echo Every minute >> my_log.txt
20:08:01.371       - [6] ...in directory: C:\laragon\bin\cronical
20:08:01.375       - [6] Process started, waiting at most 3600 seconds
20:08:02.408       - [6] Job finished
20:08:05.411       - [4] Checking services
....
20:09:00.913       - [7] Next job start 12/01/2023 20:10:00 for cmd /c echo Every minute >> my_log.txt
20:09:00.914       [5] Starting job: cmd /c echo Every minute >> my_log.txt
20:09:00.914       - [5] Executing: C:\WINDOWS\SYSTEM32\cmd.exe
20:09:00.915       - [5] ...parameters: /c echo Every minute >> my_log.txt
20:09:00.915       - [5] ...in directory: C:\laragon\bin\cronical
20:09:00.919       - [5] Process started, waiting at most 3600 seconds
20:09:01.936       - [5] Job finished
20:09:05.956       - [8] Checking services
20:09:21.098       - [8] Checking services
....

To stop the Cronical process: hold ctrl and press c. This is the standard to stop most running processes.

20:09:24.316 ***   [4] Break signaled, exiting
20:09:24.316 ***   Shutting down

Explanation #

  • The Every minute job has run on startup and every minute.

    • 20:02:32.249 [5] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:03:00.495 [9] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:04:01.069 [10] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:05:00.639 [11] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:06:01.243 [10] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:07:00.775 [9] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:08:01.370 [6] Starting job: cmd /c echo Every minute » my_log.txt
    • 20:09:00.914 [5] Starting job: cmd /c echo Every minute » my_log.txt
  • The Every five minutes job ran on startup and on the 5-minute mark. It was scheduled to run 10 minutes past the hour.

    • 20:02:32.249 [6] Starting job: cmd /c echo Every five minutes » my_log.txt
    • 20:05:00.640 [12] Starting job: cmd /c echo Every five minutes » my_log.txt
    • 20:05:00.639 - [4] Next job start 12/01/2023 20:10:00 for cmd /c echo Every five minutes » my_log.txt

Result #

The log file can be opened using notepad:

notepad my_log.txt

Notepad will launch and open my_log.txt. The output from the jobs can be seen:

Every five minutes 
Every minute 
Every minute 
Every minute 
Every five minutes 
Every minute 
Every minute 
Every minute 
Every minute 
Every minute 

Conclusion #

Setting up Cronical to run any windows job, be it a batch file, executable or php script (php.exe -f file.php) is fairly straight forward.

  1. Modify the cronical.dat to run the required jobs at the required frequency.
  2. The README has more information on how to start Cronical, install and run either manually or as a service.
The documentation states: Start Cronical from an elevated console:

For more examples on scheduling cron jobs, see the Linux man for crontab