工具

https://github.com/winsw/winsw/releases 下载.netcore的exe就行,比如
https://github.com/winsw/winsw/releases/download/v2.9.0/WinSW.NETCore31.x64.exe

PS:我为什么不下载net2那些版本,那些版本一看大小就是需要本地.netframework环境依赖的,我嫌装着麻烦,这个netcore31应该是包含了netcore运行环境的,不需要额外安装.netframework,所以我就用了这个。

下载下来,改名为
myservice.exe

配置

创建myservice.xml,主要是跟exe同名,添加内容

<service>
  
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>myapp</id>
  <!-- Display name of the service -->
  <name>myapp</name>
  <!-- Service description -->
  <description></description>
  
  <!-- Path to the executable, which should be started -->
  <executable>%BASE%\start.bat</executable>

</service>

启动服务的命令写到start.bat里就好了
start.bat比如

@echo off
cd %~dp0
java -Dspring.profiles.active=prd -jar myapp.jar

注册服务

myservice install
服务中就有一个myapp服务了

注销服务

myservice uninstall

启停服务

net start myapp
net stop myapp