一、工具介绍 WinSW 是辅助启动工具,可以将各种程序文件设置为 Windows 服务。,程序文件不符合 Windows 服务要求导致服务无法启动,比如JAR或者exe等。
二、WinSW下载地址:https://github.com/winsw/winsw/releases (无法进入可以文章底部下载)
1.sample-minimal.xml和WinSW.NET4.exe的名字改成一样(java包记得放在同一文件中)
图片1.png
2.编辑xml文件

<service>
  <!-- 服务id在windows系统中必须唯一 -->
  <id>javaService723</id>  
  <!-- 服务显示名称 -->
  <name>javaService7231727</name> 
  <!-- Service description -->
  <description>This service is a service created from a minimal configuration</description> 
  <!-- java环境变量 -->
  <env name="JAVA_HOME" value="%JAVA_HOME%" />
  <executable>java</executable>
  <!-- jar包位置, %BASE%:自动识别当前文件夹 -->
  <arguments>-jar %BASE%\admin.jar</arguments>
  
  <!-- 开机自动启动模式:Automatic(默认) -->
  <!-- 手动启动: Manual -->
  <startmode>Automatic</startmode>
  
  <!-- 日志相关配置 -->
  <logpath>%BASE%\log</logpath>
  <logmode>rotate</logmode>

</service>

图片2.png

3.添加服务
在当前文件搜索栏打开cmd窗口输入javaService.exe install并回车
图片3.png

图片4.png

**命令**
添加服务:javaService.exe install
启动服务:javaService.exe start
停止服务:javaService.exe stop
卸载服务:javaService.exe uninstall
重启服务:javaService.exe restart
查看状态:javaService.exe status

JAVA也可以按下面配置

<service>
  <id>damServer</id>
  <name>damServer</name>
  <description>jar 服务</description>
  <startmode>Automatic</startmode>
  <executable>java</executable>
  <arguments>-jar d:\java\admin.jar</arguments>
  <autoRefresh>true</autoRefresh>
</service>

下面是NGINX配置

<service>
  <id>nginxServer</id>
  <name>nginxServer</name>
  <description>nginx</description>
  <executable>d:\nginx\nginx.exe</executable>
  <stopexecutable>d:\nginx\nginx.exe -s stop</stopexecutable>
  <logpath>logs</logpath>
</service>

注册启动等 按你设置的名字启动

***.exe install 注册服务
***.exe start 启动服务
***.exe uninstall 卸载服务
***.exe stop 停止服务
***.exe restart 重启服务
***.exe status 查看服务状态

文件包提供
javaService.zip