使用 WinSW 部署 Windows Service

安装

  • winsw/winsw

Release 页面下载最新版本以及示例配置文件。

将可执行文件和配置文件都复制到自定义的目录,并将文件名改为一致。

配置

下面我使用 Frpc 作为演示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- frpc-service.xml -->
<service>
<id>frpc</id>
<name>Frpc Service</name>
<description>A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet.</description>
<executable>E:\Frp\frpc.exe</executable>
<onfailure action="restart" delay="5 sec" />
<resetfailure>1 day</resetfailure>
<arguments>-c E:\Frp\frpc.ini</arguments>
<workingdirectory>E:\Frp</workingdirectory>
<priority>AboveNormal</priority>
<stoptimeout>15 sec</stoptimeout>
<stopparentprocessfirst>false</stopparentprocessfirst>
<startmode>Automatic</startmode>
<waithint>15 sec</waithint>
<sleeptime>1 sec</sleeptime>
<logpath>E:\Frp\logs</logpath>
<log mode="roll-by-time">
<pattern>yyyyMMdd</pattern>
</log>
</service>

相关配置的解读请参考下载的示例配置文件。

运行

1
2
3
4
5
6
7
8
# 首先运行测试,看配置是否可以正常运行
./frpc-service.exe testwait

# 安装 Service
./frpc-service.exe install

# 运行 Service
./frpc-service.exe start

参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
A wrapper binary that can be used to host executables as Windows services

Usage: winsw [/redirect file] <command> [<args>]
Missing arguments trigger the service mode

Available commands:
install install the service to Windows Service Controller
uninstall uninstall the service
start start the service (must be installed before)
stop stop the service
stopwait stop the service and wait until it's actually stopped
restart restart the service
restart! self-restart (can be called from child processes)
status check the current status of the service
test check if the service can be started and then stopped
testwait starts the service and waits until a key is pressed then stops the service
version print the version info
help print the help info (aliases: -h,--help,-?,/?)

Extra options:
/redirect redirect the wrapper's STDOUT and STDERR to the specified file

WinSW 2.9.0.0
More info: https://github.com/kohsuke/winsw
Bug tracker: https://github.com/kohsuke/winsw/issues