跳至主要內容

windows程序注册成windows服务

tanmantang原创大约 2 分钟windows

摘要

windows系统中,某些程序会以命令行窗口的形式来运行,此方式窗口一关,所对应服务就会关闭,这样就很烦,所以本文将讲解如何将应用注册到windows服务当中进行管理,让应用能够驻留在WIndows服务中,从而保持一直后台运行,而且还可以定义开机自启等。

工具下载

由于windows不像Liunx系统那样配置方便,所以需要借助工具来完成注册服务

根据自身情况选择下载版本,可以选择最新版,也可以选择其他版本,然后根据系统选择对应exe文件

所有版本下载地址open in new window

以下为WinSW_v2.12.0_windows_X64下载地址:

github下载open in new window

网盘下载open in new window

新建xml配置文件

提示

xml配置文件名和下载的软件名需要在同一级目录下,并且需要有相同的名字,如

WinSW-x64.exe
WinSW-x64.xml

配置里面不要出现中文,注释除外

一个简单的xml配置

<service>
  <!-- 唯一服务ID-->
  <id>ServiceID</id>
  <!-- 显示服务的名称 -->
  <name>ServiceName</name>
  <!-- 服务描述 -->
  <description>Here you need to fill in the service description</description>
  <!-- 日志路径 -->
  <logpath>D:\application\logs\</logpath>
  <!-- 日志模式 -->
  <logmode>roll</logmode>
  <!-- 可执行文件的命令 -->
  <executable>D:\application\start.cmd</executable>
  <!-- 执行参数 -->
  <arguments>-stop true</arguments>
  <!-- 停止可执行文件的命令 -->
  <stopexecutable>D:\application\shutdown.cmd</stopexecutable>
</service>

注册服务

打开命令提示符,进入到WinSW-x64目录,执行

WinSW-x64.exe install

正常情况下,会提示安装服务成功

查看服务

打开服务,便可以看到你注册成功的服务

键盘 (windows键_ctrl旁边那个+R键) 打开运行,然后输入框输入services.msc 回车

卸载服务

打开命令提示符,进入到WinSW-x64目录,执行

WinSW-x64.exe uninstall

使用Windows PowerShell启动服务

win+x 打开Windows PowerShell

# 启动ServiceName命令
net start ServiceName
# 停止ServiceName命令
net stop ServiceName