很多时候我们需要在windows下开机运行某些服务程序:
我们可以使用自带的服务功能,但是只有程序的开发者在写程序的时候引用到这个功能,我们才能利用服务来控制程序的启动和关闭。
我们还可以使用bat脚本,把脚本添加开机启动,缺点是开机后会显示一个命令行窗口且这个窗口不能关闭。
而winsw就是一个可以将Windows上的任何一个程序注册为服务的开源软件。
软件下载地址:https://github.com/winsw/winsw
下载对应.NET版本的程序,然后创建一个对应的xml格式的配置文件,使用命令行进行安装:
安装服务
winsw install [<path-to-config>] [--no-elevate] [--user|--username <username>] [--pass|--password <password>] path-to-config:配置文件的路径。如未指定,会在可执行目录中搜索具有相同文件名的.xml文件 --no-elevate:不会自动触发UAC提示 --user|--username <username>:指定服务帐户的用户名 --pass|--password <password>:指定服务帐户的密码
卸载服务
winsw uninstall [<path-to-config>] [--no-elevate]
启动服务
winsw start [<path-to-config>] [--no-elevate]
停止服务
winsw stop [<path-to-config>] [--no-elevate] [--no-wait] [--force] --no-wait:不等待服务实际停止 --force:即使已启动相关服务也停止该服务
停止,然后启动服务
winsw restart [<path-to-config>] [--no-elevate] [--force]
检查服务状态
winsw status [<path-to-config>]
测试服务
winsw test [<path-to-config>] [--no-elevate] [--timeout <timeout>] [--no-break] 检查服务是否可以在不安装的情况下启动然后停止。 --timeout <timeout>:指定停止服务之前要等待的秒数。如果未指定或指定为-1,则WinSW会无限期地等待按键。 --no-break:忽略击键。如果指定,WinSW将等待Ctrl + C。
刷新服务属性,而无需重新安装
winsw refresh [<path-to-config>] [--no-elevate]
自定义包装可执行文件
winsw customize -o|--output <output> --manufacturer <manufacturer> -o|--output <output>:指定输出文件的路径。 --manufacturer <manufacturer>:指定自定义可执行文件的制造商名称。
绘制与服务关联的流程树
winsw dev ps [<path-to-config>] [-a|--all] -a|--all:绘制与所有服务关联的流程树。
如果服务已停止响应,则终止该服务。
winsw dev kill [<path-to-config>] [--no-elevate]
列出由当前可执行文件管理的服务
winsw dev list