Realize windows service through winsw, and realize self-starting at boot

Run Springboot packaged jar on windows, how to run it in the background and register it as a Windows service?

一、winsw (Windows Service Wrapper)

Using winsw, it can be easily realized. winsw is a small tool written in c#.
Therefore, ".NET framework" is required for operation, and now Win10 comes with .NET framework4.0

The principle is that winsw.exe itself can be registered as a windows service and can be set to start automatically.
After it is started, it executes the set command according to the configuration in the xml file with the same name as the exe to achieve the self-starting effect.

The open source address of winsw: https://github.com/winsw/winsw
The current stable version is v2.11.0, download link: https://github.com/winsw/winsw/releases/tag/v2.11.0
insert image description here
According to .Net Framework Just download the version, such as: WinSW.NET4.exe
sample-minimal.xml is a minimal configuration example.
sample-allOptions.xml is an example of all configurable parameters.

2. Check the version of .NET Framework

1. Enter "C:\Windows\Microsoft.NET\Framework" in the address bar and press Enter
2. You can see the version of .NET Framework. You can see that the highest version is 4.0
insert image description here

3. Modify and set

1. Rename winsw.exe to a meaningful name, such as apiServer.exe.
Because the exe started by the service after registration is this exe, it is easy to manage and identify after modification.
Modify sample-minimal.xml to a file with the same name as exe, such as apiServer.xml,
just put apiServer.exe and apiServer.xml in the same directory as jar.

2. xml settings

<service>
  <id>apiServer</id>
  <name>apiServer</name>
  <description>Api 服务</description>
  <startmode>Automatic</startmode>
  <executable>java</executable>
  <arguments>-jar api-proj.jar</arguments>
</service>

The relevant parameters are described as follows:

  1. id: The service ID after installing the windows service, which must be unique.
  2. name: Service name, which must also be unique. Generally, it should be the same as the id.
  3. description: service description, can use Chinese, can be used as a note.
  4. executable: The command to execute, such as the command java to start the springboot application.
  5. arguments: command execution parameters, such as package path, class path, etc.

4. Installation service

Execute cmd as an administrator, switch to the directory where the exe is located, and execute the following command.

apiServer.exe install

In addition to install, there are the following commands:

  • uninstall: remove the service
  • start: start the service
  • stop: Stop the service
  • restart: restart the service
  • status: view status

Guess you like

Origin blog.csdn.net/qgbihc/article/details/121805661