windows下使php实现守护进程

发布时间 2023-03-23 11:28:17作者: Solo李

github:

仓库地址: https://github.com/kohsuke/winsw
下载地址: https://github.com/winsw/winsw/releases

步骤:

1.先到 https://github.com/kohsuke/winsw/releases 下载对应文件,如果没有特殊要求只为实现守护进程只需要下载  sample-minimal.xml 与 WinSW.NET2.exe 或 WinSW.NET4.exe

2.在桌面新建一个文件夹(在哪里新建都可以)叫做 phpserver(名字英文,与要命名的服务名称相同,不能与已有的服务名称冲突)

服务名称=

 

 

文件夹示例:(文件夹初始只有自己下载的exe与配置文件,需要更名为与要创建的服务名称相同的名字。其他文件是我运行之后生成的)

 

 3.修改配置文件

 

 

复制代码
<!--
    Copyright (c) 2016 Oleg Nenashev and other contributors

    Permission is hereby granted, free of charge, to any person obtaining a copy of this 
    software and associated documentation files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or 
    substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
    BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->

<!--
 This is an example of a minimal Windows Service Wrapper configuration, which includes only mandatory options.
 
 This configuration file should be placed near the WinSW executable, the name should be the same.
 E.g. for myapp.exe the configuration file name should be myapp.xml
 
 You can find more information about the configuration options here: https://github.com/kohsuke/winsw/blob/master/doc/xmlConfigFile.md
 Full example: https://github.com/kohsuke/winsw/blob/master/examples/sample-allOptions.xml
-->
<service>
  
  <!--服务ID 要唯一-->
  <id>phpservice</id>
  <!-- 服务名字 -->
  <name>phpservice</name>
  <!--服务描述 -->
  <description>test php service  </description>
  
  <!-- 要运行的文件 -->
  <executable>D:\PhpStudy\php\php-7.4.1-nts\php.exe</executable>
  <!-- 要执行的命令 -->
  <arguments>D:\PhpStudy\WWW\index.php</arguments>
</service>
复制代码

4.执行命令

复制代码
// 安装服务 
phpservice.exe install
// 启动服务
phpservice.exe stop
// 停止服务
phpservice.exe start
// 重启服务(修改配置文件后要重启生效)
phpservice.exe restart
// 删除服务
phpservice.exe uninstall
复制代码

5.服务设置 (当服务崩溃的时候进行重启)

查看服务已正常运行:

 

设置服务中断后的操作:

 

主要参考

https://www.cnblogs.com/xiaqiuchu/p/12535141.html