winsw(Windows Service Wrapper)是一个开源项目,它可以让我们快速把一个可执行的程序注册为Windows的系统服务。
下载
下载页面如下: (下载地址)
需要下载可执行程序(WinSW.NET4.exe
)以及配置文件(sample-minimal.xml
)。
-
可执行程序有
WinSW.NET2.exe
以及WinSW.NET4.exe
,根据当前系统的.NET版本进行选择。如果是Win10建议使用WinSW.NET4.exe
。 -
配置文件有
sample-minimal.xml
以及sample-allOptions.xml
,sample-allOptions.xml
包括所有配置项,sample-minimal.xml
为可使用的最小配置,使用这个即可满足条件。
sample-minimal.xml
内容如下:
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>myapp</id>
<!-- Display name of the service -->
<name>MyApp Service (powered by WinSW)</name>
<!-- Service description -->
<description>This service is a service cratead from a minimal configuration</description>
<!-- Path to the executable, which should be started -->
<executable>%BASE%\myExecutable.exe</executable>
</configuration>
配置
以安装X:\Test.exe
为例,将WinSW.NE4.EXE
和sample-minimal.xml
放在文件夹(X:\winsw\
)内,将其分别重命名为Test.exe
和Test.xml
(必须同名,否则会失败),将Test.xml
修改为:
<configuration>
<!-- ID of the service. It should be unique accross the Windows system-->
<id>100001</id>
<!-- Display name of the service -->
<name>Test</name>
<!-- Service description -->
<description>Service of Test</description>
<!-- Path to the executable, which should be started -->
<executable>X:\Test.exe</executable>
</configuration>
安装
以管理员权限运行cmd,进入目录X:\winsw\
,运行命令 Test.exe install
即可完成安装。
其他常用命令如下:
- Test.exe uninstall
- Test.exe start
- Test.exe restart
- Test.exe stop
其他
如果需要设置多个程序,需要设置多个配置文件,winsw不支持一个配置文件设置多个程序。