How often do you get a PHP app that requires you to setup a cron job to run some automated task each day.
This has always been a bit of a chore with windows as there is no obviousl built in method for doing this, and the common method was to use wget. Or in my case as I use ColdFusion/Railo I would setup a scheduled task in the ColdFusion/Railo admin instead.
Today I decided to find out if there was a better way to do this this since windows 2008, and discovered that yes there is, using powershell.
Via CMD
You can use the “Schtasks” command from the CMD window.
schtasks /create /tn "my scheduled task" /tr "powershell -ExecutionPolicy unrestricted -Command \"(New-Object Net.WebClient).DownloadString(\\\http://url .to.be.executed/cron.php\\\")\" /sc DAILY /st 07:00:00 /ru System
- /sc schedule : Specifies the schedule type. Valid values are MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE.
- /st StartTime : Specifies the time of day that the task starts in HH:MM:SS 24-hour format. The default value is the current local time when the command completes. The /st parameter is valid with MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, and ONCE schedules. It is required with a ONCE schedule.
- /tn TaskName : Specifies a name for the task.
- /tr TaskRun : Specifies the program or command that the task runs. Type the fully qualified path and file name of an executable file, script file, or batch file. If you omit the path, SchTasks.exe assumes that the file is in theSystemroot\System32 directory.
- /ru {[Domain\]User | "System"} : Runs the tasks with the permission of the specified user account. By default, the task runs with the permissions of the user logged on to the computer running SchTasks.
The docs and list of commands can be found here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true
Here is how to do it via GUI.
Just create a task in the normal way, under “Action” choose “start a program” –> powershell
And then under arguments you specify the following
-ExecutionPolicy unrestricted -Command "(New-Object Net.WebClient).DownloadString(\"http://www.yoururl.com/cron.php\")"
Recent Comments