winsw
Contents
WinSW 服务管理
有时候需要将bat、exe等文件作为Windows的服务,可以使用bat、nssm等工具将此类文件设置为Windows服务,但使用winsw可以更简单。
使用winsw 注册成系统的服务后默认开启开机自启
1、下载地址
https://github.com/winsw/winsw/releases
2、配置示例
以下已frp作为配置示例介绍
下载下来的WinSW-x64.exe文件复制到frpc的安装目录并重命名(方便写命令,如service.exe)
在frpc的安装目录中新建一个名称为service的xml文件(必须要和WinSW-x64.exe重命名的service名称一致),目的是WinSW会去读取和自己相同名称的xml文件中的配置进行相关设置,xml文件中的具体配置如下所示:
<service>
<!-- 服务ID名称(唯一) -->
<id>Frpc-Server</id>
<!-- 服务显示名称 -->
<name>Frpc-Server</name>
<!-- 服务的描述信息 -->
<description>Frpc客户端</description>
<!-- 可设置环境变量 -->
<env name="HOME" value="%BASE%"/>
<!-- 要执行的可执行文件 -->
<executable>%BASE%\frpc.exe</executable>
<!-- 可执行文件传递的参数 -->
<!-- <arguments>server "%BASE%\data"</arguments> -->
<log mode="roll-by-size-time">
<!-- 单位为KB,默认值10MB -->
<sizeThreshold>102400</sizeThreshold>
<pattern>yyyyMMdd</pattern>
<autoRollAtTime>00:00:00</autoRollAtTime>
</log>
</service>
<service>
<!-- 该服务的唯一标识 -->
<id>frp</id>
<!-- 该服务的名称 -->
<name>frpc.exe</name>
<!-- 该服务的描述 -->
<description>frpc客户端 这个服务用 frpc 实现内网穿透</description>
<!-- 设置环境变量 -->
<env name="HOME" value="%BASE%"/>
<!-- 要运行的程序路径 -->
<executable>D:\frp\frpc.exe</executable>
<!-- 携带的参数 -->
<arguments>-c frpc.ini</arguments>
<!-- 第一次启动失败 60秒重启 -->
<onfailure action="restart" delay="60 sec"/>
<!-- 第二次启动失败 120秒后重启 -->
<onfailure action="restart" delay="120 sec"/>
<!-- 日志模式 -->
<logmode>append</logmode>
<!-- 指定日志文件目录(相对于executable配置的路径) -->
<logpath>logs</logpath>
</service>
yaml格式
service.yaml
id: jenkins
name: Jenkins
description: This service runs Jenkins continuous integration system.
env:
- name: JENKINS_HOME
value: "%BASE%"
executable: java
# arguments: arg1 arg2 arg3
arguments: >
-Xrs
-Xmx256m
-jar "%BASE%\jenkins.war"
--httpPort=8080
log:
mode: roll-by-size
logpath: '%BASE%\log'
# 100M
sizeThreshold: 102400
keepFiles: 5
depend:
- Eventlog
- W32Time
onFailure:
- action: restart
delay: 10 sec
workingdirectory: '%BASE%\workdir'
目录: D:\frp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2022/11/10 15:36 logs
-a---- 2022/11/10 9:54 11677696 frpc.exe
-a---- 2022/11/10 15:20 451 frpc.ini
-a---- 2022/11/10 15:34 17462251 service.exe
-a---- 2022/11/10 15:34 831 service.xml
3、服务管理
打开CMD
或Powershell
进行安装服务
# 安装服务
PS D:\frp> .\service.exe install
2022-11-10 15:45:11,601 INFO - Installing service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:11,654 INFO - Service 'Frpc-Server (Frpc-Server)' was installed successfully.
# 启动服务
PS D:\frp> .\service.exe start
2022-11-10 15:45:20,538 INFO - Starting service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:21,705 INFO - Service 'Frpc-Server (Frpc-Server)' started successfully.
# 查看服务状态
PS D:\frp> .\service.exe status
Started
# 重启服务
PS D:\frp> .\service.exe restart
2022-11-10 15:45:41,415 INFO - Stopping service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:41,703 INFO - Starting service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:43,255 INFO - Service 'Frpc-Server (Frpc-Server)' restarted successfully.
# 停止服务
PS D:\frp> .\service.exe stop
2022-11-10 15:45:51,015 INFO - Stopping service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:51,033 INFO - Service 'Frpc-Server (Frpc-Server)' stopped successfully.
# 卸载服务
PS D:\frp> .\service.exe uninstall
2022-11-10 15:45:57,827 INFO - Uninstalling service 'Frpc-Server (Frpc-Server)'...
2022-11-10 15:45:57,839 INFO - Service 'Frpc-Server (Frpc-Server)' was uninstalled successfully.
# 查看help命令
PS D:\frp> .\service.exe --help
A wrapper binary that can be used to host executables as Windows services
Usage: winsw <command> [<args>]
Missing arguments triggers 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.11.0.0
More info: GitHub - winsw/winsw: A wrapper executable that can run any executable as a Windows service, in a pe
Bug tracker: https://github.com/winsw/winsw/issues
4、windows 服务中检查
Win + R 打开运行 –> 输入 services.msc
5、WinSW命令
命令 | 描述 |
---|---|
install | 安装服务 |
uninstall | 卸载服务 |
start | 启动服务 |
stop | 停止服务 |
restart | 重启服务 |
status | 检查服务状态 |
refresh | 刷新服务属性而不是重新安装 |
customize | – |