将PHP脚本设置为Windows服务 [英] Setting a PHP script as a Windows Service

查看:227
本文介绍了将PHP脚本设置为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将PHP脚本设置为Windows服务.

I need to set up a PHP script as a windows service.

无论哪个用户登录,我都需要它运行,并且在系统启动时-听起来Windows服务是最好的,但是很高兴听到其他建议.

I need it to run regardless of which user is logged in, and on system start up - so it sounds like a windows service is best, but am happy to hear other suggestions.

(此脚本连续运行,这不是我可以使用计划任务管理器"执行的每5分钟运行一次"的事情.)

(This script runs continuously, it's not a "run every 5 mins" thing I could use the Scheduled Task Manager for.)

http://support.microsoft.com/kb/251192 涵盖了使用sc的过程. exe程序来安装服务.

http://support.microsoft.com/kb/251192 covers using the sc.exe program to install your service.

但是从我读到的内容来看,我需要对PHP脚本进行包装,以接受来自Windows服务管理器的特殊命令.有人可以帮忙吗?

But from what I've read, I need to have a wrapper round the PHP script to accept the special commands from the windows service manager. Can anyone help with this?

推荐答案

也许资源工具包工具(特别是srvany.exe)可以为您提供帮助. MSDN:如何创建用户定义的服务,可能还包括

Maybe the Resource Kit Tools (specifically srvany.exe) can help you here. MSDN: How To Create A User-Defined Service and possibly this hint for 2008 Server should help you setup any executable as a service. (I've successfully used this on Windows 2003 Server, Windows 2008 Server and on Windows XP Professional [other Resource Kit, though])

您将创建一个包含php your-script.php的蝙蝠,并用srvany.exe和voila对其进行包装,一旦计算机加载了服务,便会启动该脚本.

You'd create a bat containing php your-script.php, wrap that with srvany.exe and voila, the script is started once the machine loads the services.

srvany.exe应该处理那些希望守护程序执行的启动/停止/重新启动调用.它将在启动时加载可执行文件,在停止时终止进程,在重新启动时都执行.因此,您不必担心这部分.您可能想检查 register_shutdown_function()是否可以帮助确定服务进程何时终止.

srvany.exe should handle those start/stop/restart calls you'd expect a daemon to execute. It would load your executable on start, kill the process on stop, do both on restart. So you don't have to worry about this part. You might want to check if a register_shutdown_function() can help identify when your service process is killed.

您甚至可以定义对其他服务的依赖关系(例如某些数据库或某些此类服务).

You can even define dependencies to other services (say some database or some such).

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\THENAMEOFYOURSERVICE]
"DependOnService"="DEPENDONTHIS"

THENAMEOFYOURSERVICE替换为您提供的服务的名称,将DEPENDONTHIS替换为要依赖的服务的名称(例如"Postgres9.0"或类似名称).将该文件保存到dependency.reg并用regedit /s dependency.reg加载. (或在资源管理器中双击它……)

replace THENAMEOFYOURSERVICE with the name you gave your service and DEPENDONTHIS with the name of the service to depend on (say "Postgres9.0" or something). Save that file to dependency.reg and load it with regedit /s dependency.reg. (Or doubleclick it in explorer…)

这篇关于将PHP脚本设置为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆