在Windows服务器上,我们有一个偶尔运行的任务,使用“计划任务”来控制它。 (最好不要直接运行它,因为它可能需要以特定用户身份运行,并且计划任务配置具有所有适当的密码)。
我们希望通过 PHP 脚本来控制它,以便网站的访问者可以请求运行该任务。 PHP可以触发定时任务吗?包括在任务已运行时给出错误或不采取任何操作。
注意:锁定文件等可能不是一个有用的解决方案,因为任务可能在完成之前失败/崩溃
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
发布评论
评论(2)
一个简单的方法:让您的任务一直运行,但在找到某个特定文件之前不执行任何操作,然后将其删除并完成其工作(然后返回等待)。
你的PHP(或其他)脚本只需要创建该文件......
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 ...
如果您可以控制 PHP 环境,那么您可以使用 shell_exec 并带有附加参数。您还可以在相关任务中使用类似互斥体的检查来防止多次执行。
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.