前言
- wsw 2.3.0
Windows Service Wrapper
WinSW is an executable binary, which can be used to wrap and manage a custom process as a Windows service. Once you download the installation package, you can rename winsw.exe to any name, e.g. myService.exe.
Windows Service Wrapper 可以将 exe应用、Java 的 jar 包等注册为 windows 服务。
项目地址:
- https://github.com/kohsuke/winsw
- https://github.com/winsw/winsw
下载 wsw 2.3.0
github上的下载地址:
https://github.com/kohsuke/winsw/releases/tag/winsw-v2.3.0
连不上github可以试试这个下载地址:
http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/2.3.0/
下载WinSW.NET4.exe 和 sample-minimal.xml:
https://github.com/kohsuke/winsw/releases/download/winsw-v2.3.0/WinSW.NET4.exe
https://github.com/kohsuke/winsw/releases/download/winsw-v2.3.0/sample-minimal.xml
配置
以MyApp.exe的配置为例进行说明。
把WinSW.NET4.exe和exe可执行程序在同一个目录下。并将WinSW.NET4.exe更改名称,一般与exe可执行程序名一致。例如,将WinSW.NET4.exe改为MyApp_service.exe。
把sample-minimal.xml改名为MyApp_service.xml。
更改 MyApp_service.xml 配置文件为下面这样:
<configuration>
<!-- ID of the service. It should be unique across the Windows system-->
<id>MyApp</id>
<!-- Display name of the service -->
<name>MyApp</name>
<!-- Service description -->
<description>MyAppService (powered by WinSW)</description>
<!-- Path to the executable, which should be started -->
<executable>D:\MyApp\MyApp.exe</executable>
</configuration>
注册/卸载服务
以管理员身份运行 CMD 命令窗口,输入命令注册为 windows 服务
MyApp_service.exe install # 注册服务
MyApp_service.exe uninstall # 卸载服务
启动/停止服务
在 CMD 命令窗口,输入命令启动服务,或我的电脑右键管理,在服务列表中启动服务。
MyApp_service.exe start # 启动服务
MyApp_service.exe stop # 停止服务
MyApp_service.exe restart # 重启服务
MyApp_service.exe status # 查看服务运行状态
另一组可以启动/停止服务的命令
net start MyApp # 启动服务
net stop MyApp # 停止服务
net stop MyApp && net start MyApp # 重启服务