使用 WinSW 把可执行文件安装为服务

https://github.com/winsw/winsw

参数列表

Command Description
install Installs the service.
uninstall Uninstalls the service.
start Starts the service.
stop Stops the service.
restart Stops and then starts the service.
status Checks the status of the service.
refresh Refreshes the service properties without reinstallation.
customize Customizes the wrapper executable.
dev ps Draws the process tree associated with the service.
dev kill Terminates the service if it has stopped responding.
dev list Lists services managed by the current executable.

使用方式

先说结论

  • WinSW.exemyapp.xml 不在同一目录

    • 使用 WinSW.exe install .\myapp.xml 操作服务
  • WinSW.exemyapp.xml 在同一目录

    • 可以使用 WinSW.exe install .\myapp.xml 操作服务
    • 也可以把 WinSW.exe 重命名为 myapp.exe,然后使用 myapp install 操作服务

服务示例: chfs-service.xml

1
2
3
4
5
6
7
8
9
10
11
12
<service>
<id>chfs-service</id>
<!-- name 和 description 字段可以省略 -->
<name>CUTE HTTP FILE SERVER</name>
<description> http://iscute.cn/chfs </description>
<!-- 下面的可执行文件可以放到当前目录也可以放到其他目录,放到其他目录就要用绝对地址 -->
<executable>D:\WIN-WORKSPACE\scoop\apps\chfs\current\chfs.exe</executable>
<arguments> --rule="::r|root:123456:rwd" --path="D:\\" </arguments>
<!-- 把日志文件都放到xml所在位置的子目录 -->
<logpath>%BASE%\logs</logpath>
<log mode="roll" />
</service>

全局方式

  1. 发布页面下载 WinSW.exe
  2. 创建一个专门用来放 xml 文件的目录,把 WinSW.exe 放入其中,创建 myapp1.xmlmyapp2.xml ...
  3. 然后可以使用形似下面的命令操作每个服务
1
2
WinSW.exe install .\myapp1.xml
WinSW.exe install .\myapp2.xml

也可以随便找个其他地方,把 WinSW.exe 放入其中,并加入 PATH,总之能访问到 WinSW.exe 就行

捆绑方式

  1. 下载 WinSW.exe
  2. 创建一个文件夹,把 WinSW.exe 放入其中并重命名为 myapp.exe
  3. 在相同文件夹内创建 myapp.xml
  4. 然后可以使用形似下面的命令操作服务
1
2
3
4
myapp install
myapp start
myapp stop
myapp uninstall

目录结构:

1
2
├── chfs-service.exe
└── chfs-service.xml

使用 WinSW 把可执行文件安装为服务
https://blog.oopsky.top/post/376ebc48/
作者
waleslau
发布于
2023年1月5日
许可协议