Windows系统服务自动注册程序工具WinSW

系统 Windows
其实Windows系统自带后台程序管理的功能,也就是我们经常用到的服务。但是Windows的服务只有程序的开发者在写程序的时候引用到这个功能,才能利用服务来控制程序的启动和关闭。对于一般的命令行程序来说,没办法利用服务。

概述

在现实生活中,使用Windows系统的电脑的时候,可能会遇到这么一种情况:想把一些应用程序添加为开机启动项。对于有图形界面的应用程序,一般不存在问题。但是如果想运行命令行应用程序,就不是那么方便了。一种笨办法就是写个bat,放到启动文件夹里,就可以开机启动了。开机之后,你就会发现,这样会一直显示着一个CMD窗口,而且这个窗口不能关,关了程序就停了。

其实Windows系统自带后台程序管理的功能,也就是我们经常用到的服务。但是Windows的服务只有程序的开发者在写程序的时候引用到这个功能,才能利用服务来控制程序的启动和关闭。对于一般的命令行程序来说,没办法利用服务。

今天我们要介绍的WinSW,它就是一个可以将Windows上的任何一个程序注册为服务的工具。同样也可以进行卸载该服务。

安装

首先要下载winsw。它是一个单个的可执行文件,我们到Github release这里就可以下载winsw了。一般来说当然是下载最新的。winsw可以运行在.NET2和.NET4两个版本上,当然如果使用Win10等比较新的系统。

下载地址:https://github.com/winsw/winsw/releases

这里我们下载v2.12.0版本,下载链接地址

https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-x64.exe
  • 1.

配置文件

这里我们将WinSW.exe放到E:\service目录下,然后在WinSW.exe同级的目录下,新建winsw.xml文件,输入以下内容。

winsw.xml文件内容

<?xml version="1.0" encoding="UTF-8" ?>
<service>
    <id>TinywanFrpServer0.54.0</id>
    <name>Tinywan Frp Server 0.54.0 </name>
    <description>This service runs frp reverse proxy.</description>
    <executable>E:\frp_0.54.0_windows_amd64\frpc.exe</executable>
    <arguments>-c E:\frp_0.54.0_windows_amd64\frpc.toml</arguments>
    <logmode>reset</logmode>
</service>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

最终E:\service目录下配置和可执行文件

WinSW.exe  winsw.xml
  • 1.

注册服务

配置文件编写完之后,将配置文件与WinSW.exe放在同一目录中。注意对应WinSW.exe的配置文件名称应该是WinSW.xml。此时,WinSW.exe、WinSW.xml以及你的应用程序应该都是在同一目录中。然后用管理员权限打开一个命令提示符窗口,cd进入到应用程序所在目录,可以通过输入下面的命令来进行控制应用程序对应的服务:

然后打开管理员权限命令提示符,使用下面的命令安装。

$ ./WinSW.exe install
2024-08-29 14:56:43,604 INFO  - Installing service 'Tinywan Frp Server 0.54.0  (TinywanFrpServer0.54.0)'...
2024-08-29 14:56:43,648 INFO  - Service 'Tinywan Frp Server (frp)' was installed successfully.
  • 1.
  • 2.
  • 3.

启动服务

$ ./WinSW.exe start
2024-08-29 15:11:40,418 INFO  - Starting service 'Tinywan Frp Server 0.54.0  (TinywanFrpServer0.54.0)'...
2024-08-29 15:11:40,976 INFO  - Service 'Tinywan Frp Server 0.54.0  (TinywanFrpServer0.54.0)' started successfully.
  • 1.
  • 2.
  • 3.

安装服务命令执行后,如果返回值为0,就表示服务已经安装成功。此时在windows服务的窗口,就能看到你刚才安装的服务了

如果服务启动有问题,可以通过错误日志文件WinSW.wrapper.log查看详情

2024-08-29 14:58:46,486 ERROR - Failed to start service.
System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'E:\frp_0.54.0_windows_amd64\frpc.exe' with working directory 'E:\service'. 系统找不到指定的文件。
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo )
   at System.Diagnostics.Process.StartCore(ProcessStartInfo )
   at System.Diagnostics.Process.Start()
   at WinSW.Util.ProcessHelper.StartProcessAndCallbackForExit(Process processToStart, String executable, String arguments, Dictionary`2 envVars, String workingDirectory, Nullable`1 priority, ProcessCompletionCallback callback, LogHandler logHandler, Boolean hideWindow)
   at WinSW.WrapperService.StartProcess(Process processToStart, String arguments, String executable, LogHandler logHandler)
   at WinSW.WrapperService.DoStart()
   at WinSW.WrapperService.OnStart(String[] args)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

Nacos注册为Windows服务

下载Nacos

二进制包下载地址:https://nacos.io/download/nacos-server

nacos-service.xml配置文件

<service>
    <!-- 唯一服务ID -->
    <id>NacosServer</id>
    <!-- 显示服务的名称 -->
    <name>Nacos Server 2.4.2 </name>
    <!-- 服务描述 -->
    <description>This service runs Nacos Server </description>
    <!-- 日志路径 -->
    <logpath>你的nacos路径\nacos\bin\logs\</logpath>
    <!-- 日志模式 -->
    <logmode>roll</logmode>
    <!-- 可执行文件的命令 -->
    <executable>E:\service\nacos\nacos\bin\startup.cmd</executable>
    <!-- 停止可执行文件的命令 -->
    <stopexecutable>E:\service\nacos\nacos\bin\shutdown.cmd</stopexecutable>
</service>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

安装Nacos Service服务

nacos-service.exe install

2024-09-05 19:46:32,748 INFO  - Installing service 'Nacos Server 2.4.2  (NacosServer)'...
2024-09-05 19:46:32,805 INFO  - Service 'Nacos Server 2.4.2  (NacosServer)' was installed successfully.
  • 1.
  • 2.
  • 3.
  • 4.

开启服务

nacos-service.exe start
2024-09-05 19:48:48,307 INFO  - Starting service 'Nacos Server 2.4.2  (NacosServer)'...
2024-09-05 19:48:48,331 INFO  - Service 'Nacos Server 2.4.2  (NacosServer)' has already started.
  • 1.
  • 2.
  • 3.

查看Nacos Service服务

图片图片

常用命令

winsw install 安装服务

winsw uninstall 卸载服务

winsw start 开启服务

winsw stop 停止服务

winsw restart 重新启动服务

winsw status 检查服务的当前状态
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
责任编辑:武晓燕 来源: 开源技术小栈
相关推荐

2022-04-26 05:36:42

服务治理模式

2010-06-02 17:16:16

自动运行SVN

2010-05-21 13:23:23

SVN作为window

2009-08-14 15:19:38

Windows服务程序Windows服务

2021-07-15 06:34:07

Zabbix监控Windows

2019-07-07 09:20:02

Windows 10Windows备份注册表

2009-02-25 08:41:49

Windows 7自动安装工具更新

2010-05-26 08:37:45

启动SVN

2018-05-25 15:26:28

Windows 10Windows自动启动

2024-03-13 07:49:15

.NET依赖注入DI

2021-07-29 23:21:01

Windows 8Windows微软

2011-03-30 09:03:33

2009-08-14 15:06:08

Windows服务程序

2009-08-14 11:00:16

C#创建Windows

2009-08-14 15:47:18

C#Windows服务

2009-08-14 14:25:09

Windows服务程序

2011-05-17 14:53:35

C

2024-07-09 17:09:49

C#开发Windows窗体

2009-08-14 13:41:13

C#Windows服务

2010-04-21 14:33:05

Windows注册表
点赞
收藏

51CTO技术栈公众号