views:

79

answers:

2

On a Windows server, we have a task which runs occasionally, using "Scheduled Tasks" to control it. (best not to run it directly, since it might need to be run as a particular user, and the Scheduled Task configuration has all the appropriate passwords).

We'd like to control this via a PHP script, so that visitors to a website can request the task be run. Can PHP trigger the scheduled task? Including giving an error or taking no action if the task is already running.

Note: lockfiles etc. probably not a useful solution, since the task could fail/crash before completing

A: 

If you have control of PHP environment, then you can directly execute external application within PHP using shell_exec and with additional parameters. Also you can use mutex like checks in the related task to prevent multiple executions.

whoi
(a) how does this allow running as a different username, using the other user's Windows configuration(b) how does this check whether the task is already running?
OJW
+2  A: 

An easy way : let your task run all the time but do nothing till it finds some specific file, then delete it and do its job (then go back to wait).

You PHP (or whatever) script just have to create that file ...

siukurnin
of course the `while(true)` loop should have a Sleep statement in there somewhere so it waits before the next check of the file.
Agent_9191
good answer, although if the task crashed then it would need something to restart it. Which comes back to: how would the thing which restarts it know whether the task is running?
OJW