将 PHP 脚本设置为 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5952500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a PHP script as a Windows Service
提问by James
I need to set up a PHP script as a windows service.
我需要将 PHP 脚本设置为 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.
无论哪个用户登录和系统启动,我都需要它运行 - 所以听起来 Windows 服务是最好的,但我很高兴听到其他建议。
(This script runs continuously, it's not a "run every 5 mins" thing I could use the Scheduled Task Manager for.)
(这个脚本连续运行,它不是我可以使用计划任务管理器的“每 5 分钟运行一次”。)
http://support.microsoft.com/kb/251192covers using the sc.exe program to install your service.
http://support.microsoft.com/kb/251192介绍了如何使用 sc.exe 程序安装您的服务。
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?
但是从我读过的内容来看,我需要对 PHP 脚本进行包装以接受来自 Windows 服务管理器的特殊命令。有人能帮忙吗?
采纳答案by rodneyrehm
Maybe the Resource Kit Tools(specifically srvany.exe) can help you here. MSDN: How To Create A User-Defined Serviceand possibly this hint for 2008 Servershould help you setup anyexecutable as a service. (I've successfully used this on Windows 2003 Server, Windows 2008 Server and on Windows XP Professional [other Resource Kit, though])
也许资源工具包工具(特别是 srvany.exe)可以帮助您。MSDN: How To Create A User-Defined Service和2008 Server 的可能提示应该可以帮助您将任何可执行文件设置为服务。(我已经成功地在 Windows 2003 Server、Windows 2008 Server 和 Windows XP Professional [其他资源工具包] 上使用了它)
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.
您将创建一个包含 bat php your-script.php
,用 srvany.exe 包装它,瞧,一旦机器加载服务,脚本就会启动。
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.
srvany.exe 应该处理那些您希望守护进程执行的启动/停止/重启调用。它会在启动时加载您的可执行文件,在停止时终止进程,在重新启动时执行这两项操作。所以你不必担心这部分。您可能想要检查register_shutdown_function() 是否可以帮助确定您的服务进程何时被终止。
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"
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…)
替换THENAMEOFYOURSERVICE
为您为服务DEPENDONTHIS
提供的名称以及要依赖的服务名称(例如“Postgres9.0”或其他名称)。将该文件保存到dependency.reg
并加载它regedit /s dependency.reg
。(或在资源管理器中双击它...)
回答by abaumg
We used FireDaemonfor this task, it doesn't require wrapper scripts etc. Unfortunately it's not freeware.
我们使用FireDaemon来完成这项任务,它不需要包装脚本等。不幸的是,它不是免费软件。
回答by David
You can run php on command line by giving different parameters and also the script-file as parameter. If you add the whole line you need into the service configuration it should run. So you are also able to try the device before creating the service. If the php-script is outside your web-route perhaps you should add the folder to the PATH-Variable of windows.
您可以通过提供不同的参数以及脚本文件作为参数在命令行上运行 php。如果将所需的整行添加到服务配置中,它应该运行。因此,您还可以在创建服务之前试用该设备。如果 php 脚本在您的网络路由之外,也许您应该将该文件夹添加到 Windows 的 PATH-Variable 中。
回答by James
I found this but haven't tried it myself. PHP actually comes with some functions to do this: http://uk.php.net/manual/en/book.win32service.php
我发现了这个,但我自己没有尝试过。PHP实际上带有一些功能来做到这一点:http: //uk.php.net/manual/en/book.win32service.php
Here are some examples: http://uk.php.net/manual/en/win32service.examples.php
以下是一些示例:http: //uk.php.net/manual/en/win32service.examples.php
<?php
if ($argv[1] == 'run') {
win32_start_service_ctrl_dispatcher('dummyphp');
while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
# do your work here.
# try not to take up more than 30 seconds before going around the loop
# again
}
}
?>
回答by Virgili Garcia
After a few days... i found this magnific option!
几天后......我找到了这个很棒的选择!
He build an .exe that recive the service options and works fine!
他构建了一个 .exe 来接收服务选项并且工作正常!
https://superuser.com/questions/628176/php-cgi-exe-as-a-windows-service/643676#643676
https://superuser.com/questions/628176/php-cgi-exe-as-a-windows-service/643676#643676
the command correct:
命令正确:
sc create FOO binPath= "service.exe \"C:\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini"\" type= own start= auto error= ignore DisplayName= "FOO php"
sc create FOO binPath="service.exe \"C:\php\php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini"\" type= own start= auto error= ignore DisplayName = "FOO php"
回答by Andris
NSSM - the Non-Sucking Service Manageris also a solution, and then
NSSM——Non-Sucking Service Manager也是一个解决方案,然后
nssm install PHP php-cgi.exe -b 127.0.0.1:9000 -c C:\php\php.ini