Run Cron Job on PHP Script, on localhost in Windows
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php
)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:
- Open Task Scheduler from windows Start menu
- Go to Action menu and hit Create Task...
- in General tab, fill the Name and Description fields as you want
- in Triggers tab, hit New button.
- from Begin the Task dropdown, select On a schedule and choose Daily
- from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
- on Actions tab, from Action dropdown, select Start a program.
- on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
- leave Add argumentts (optional) section empty.
- in Start in (optional) box, enter parent directory of shellscript.vbs like
C:\path\to\
. - Hit upvote on this tutorial :) Have fun.
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
https://www.drupal.org/node/31506
If you use answer from Trix and get same problem as Metafaniel:
I have a problem, with this procedure, the task is beinge executed, however my php script it's not running, I got the "Open with" dialogue. If I see the properties of my task, it states: ActionName C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as you suggested. What am I doing wrong?
you should use this:
- Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
- Instead "leave Add argumentts (optional) section empty" use (with quotes): "C:\path\to\shellscript.vbs"
After many failed attempts at using Windows Task Scheduler this is the method that worked for me on my Windows 10 Professional machine. I just skipped adding a .vbs and .bat file and posted the PATH to the script file directly.
Under "Actions > Settings" Where it gives the option to browse for a "Program/Script" that's where I put the full PATH AND Filename to the script I wanted to run WITHOUT quotes.
C:\Server\Scripts\script.py
Then in the same area under "Actions > Settings" right below the text box where you just entered your "Program/Script", it has the area that says "Start in (optional)". That's where I put the PATH to the program that runs the script (ie: python.exe in this instance). But I did not specify "python.exe" directly I used the PATH ONLY without quotes like shown below.
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
If you installed Python directly without Visual Studio it will be under a different path probably something like "C:\Program Files\Python\Python37" or something similar if I remember correctly...