By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Automatically trigger cron on drupal sites from Windows

How to automate manual cron runs on Drupal sites from a Windows computer using AutoIt.

Edited: 2015-12-01 22:30

AutoIt Logo

In some cases we might want to run Cron manually on a Drupal site. This is typically done by visiting a URL with a browser. The exact URL should be found on the Cron settings page of the Drupal installation, and it likely looks like the below:

 http://example.com/cron.php?cron_key=1iLutnD4nuThFpLkSjSBKor-7P8htRQXLp3yYE3lmBo

There is another way to run Cron manually which does not require the use of a browser, which is to send a HTTP GET request to the URL from a scripting language. Since the URL does not change, we can easily create our own tool to run Cron automatically, and then leave the automation setting for Cron to never inside Drupal itself.

On a Linux computer you can easily setup a Cron job to perform a GET request with cURL (or whatever). On Windows it may be easier to use a scripting language such as AutoIt, rather than scheduling a task in Windows task manager.

There are at least two ways to accomplish this, either you create a script in AutoIt which you can then add to Windows Task Scheduler, or you can eliminate WTS altogether, and instead manage everything from within AutoIt.

AutoIt script to automate manual Cron runs

Below is a sample script file, which will automatically run Cron once every thirty minutes Note. that the time units are provided in milliseconds.

#comments-start
 This AutoIt Script is intended to automate the manual execution of Cron on Drupal powered sites

 The exit hotkey for this script is CTRL+ALT+x
#comments-end


; Maximum number of times to run Cron
$maxRuns = 27

; The Script Below

;Assigns the HotKey to our custom exit function
HotKeySet("^!x", "MyExit")
$i = 0
While $i <= $maxRuns
sleep(1810000) ; Milliseconds

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET", "http://example.com/cron.php?cron_key=1iLutnD4nuThFpLkSjSBKor-7P8htRQXLp3yYE3lmBo", False)

$oHTTP.Send()
; $oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

$i = $i+1
WEnd

The script uses the winhttp.winhttprequest.5.1 COM Object to send its HTTP requests. In case you need to debug the code above, simply uncomment the $oReceived variable, and show it in a MsgBox.