Windows [Tool 04] WinSW official website instructions and example sharing (register exe and jar as services) to achieve automatic service restart after server restart

Official Github ; official download address . It is difficult to download without Git acceleration. Let me share the latest stable version of v2.12.0the network disk connection with the release date of 2023.01.29.
Included files:

  • WinSW-x64.exe
  • sample-minimal.xml
  • sample-allOptions.xml

Link: https://pan.baidu.com/s/1sN3hL5HvFzzNwuz8npaQNw
Extraction code: vsvg

Why register as a service: The service can restart itself after the server is restarted.

1. Instructions for use on the official website

1.1 Configuration instructions for use

Use WinSW as a global tool

  1. Take WinSW.exe or WinSW.zip from the distribution.
  2. Write myapp.xml (see the XML config file specification and samples for more details).
  3. Run winsw install myapp.xml [options] to install the service.
  4. Run winsw start myapp.xml to start the service.
  5. Run winsw status myapp.xml to see if your service is up and running.

Used as a global tool, different services use different xml files for operations.

Use WinSW as a bundled tool

  1. Take WinSW.exe or WinSW.zip from the distribution, and rename the .exe to your taste (such as myapp.exe).
  2. Write myapp.xml (see the XML config file specification and samples for more details).
  3. Place those two files side by side, because that’s how WinSW discovers its co-related configuration.
  4. Run myapp.exe install [options] to install the service.
  5. Run myapp.exe start to start the service.

When used as a binding tool, the xml file with the same name is used by default for operation. I personally feel that this method is more suitable for implementation partners.

Sample configuration file

You write the configuration file that defines your service. The example below is a primitive example being used in the Jenkins project:

<service>
  <id>jenkins</id>
  <name>Jenkins</name>
  <description>This service runs Jenkins continuous integration system.</description>
  <env name="JENKINS_HOME" value="%BASE%"/>
  <executable>java</executable>
  <arguments>-Xrs -Xmx256m -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>
  <log mode="roll"></log>
</service>

The full specification of the configuration file is available here . You can find more samples here .
This configuration file example is a typical executable file + parameters, suitable for many services.

1.2 Usage

WinSW is being managed by the XML configuration file.
Your renamed WinSW.exe binary also accepts the following commands:

Command Description
install Installs the service.
uninstall Uninstalls the service.
start Starts the service.
stop Stops the service.
restart Stops and then starts the service.
status Checks the status of the service.
refresh Refreshes the service properties without reinstallation.
customize Customizes the wrapper executable.
dev Experimental commands.

Experimental commands:

Command Description
dev ps Draws the process tree associated with the service.
dev kill Terminates the service if it has stopped responding.
dev list Lists services managed by the current executable.

Most commands require Administrator privileges to execute. WinSW will prompt for UAC in non-elevated sessions.
These commands will not be explained in detail and will be introduced when used.

2.Example sharing

2.1 Register the exe as a service

Here we use object storage MinIO minio.exeas Use WinSW as a bundled toolan example. The detailed steps are as follows:

  1. will be WinSW-x64.exerenamed tominio-server.ext
  2. The content of the added configuration file minio-server.ximis as follows, and the configuration details can be viewedsample-allOptions.xml
<service>
  <id>minio-server</id>
  <name>MinIO-Server</name>
  <description>This service runs MINIO OBJECT STORE.</description>
  <env name="MINIO_HOME" value="%BASE%"/>
  <executable>%BASE%\minio.exe</executable>
  <arguments>server D:\minio_data --console-address ":9001"</arguments>
  <logpath>%BASE%\logs</logpath>
  <log mode="roll-by-size-time">
    <sizeThreshold>1024</sizeThreshold>
    <pattern>yyyyMMdd</pattern>
    <autoRollAtTime>00:00:00</autoRollAtTime>
    <zipOlderThanNumDays>5</zipOlderThanNumDays>
    <zipDateFormat>yyyyMMdd</zipDateFormat>
  </log>
  <env name="MINIO_ROOT_USER" value="admin" />
  <env name="MINIO_ROOT_PASSWORD" value="admin123" />
</service>
  1. cmdExecute the command minio-server.exe installto install it as a service (the service is not started at this time)
INFO  - Installing service 'MinIO-Server (minio-server)'...
INFO  - Service 'MinIO-Server (minio-server)' was installed successfully.
  1. Execute the command minio-server.exe startto start the service

Complete process test:

Insert image description here

2.2 Register jar as a service

math-game.jarHere we use Arthas (Alsace) package for testing Use WinSW as a bundled toolas an example. The detailed steps are as follows:

  1. will be WinSW-x64.exerenamed tomath-game-server.exe
  2. Add the configuration file math-game-server.xmlcontent as follows
<service>
  <id>math-game-server</id>
  <name>Math-Game-Server</name>
  <description>This service runs math-game server.</description>
  <env name="MATHGAME_HOME" value="%BASE%"/>
  <executable>java</executable>
  <arguments>-Xrs -Xmx128m -jar "%BASE%\math-game.jar"</arguments>
  <logpath>%BASE%\logs</logpath>
  <log mode="roll-by-size-time">
    <sizeThreshold>1024</sizeThreshold>
    <pattern>yyyyMMdd</pattern>
    <autoRollAtTime>00:00:00</autoRollAtTime>
    <zipOlderThanNumDays>5</zipOlderThanNumDays>
    <zipDateFormat>yyyyMMdd</zipDateFormat>
  </log>
</service>

The -Xrs parameter is short for "Reduce Signal Usage", which tells the JVM to reduce the use of operating system signals. Normally, the JVM captures some operating system signals, such as SIGTERM (termination signal) and SIGINT (interrupt signal), to gracefully shut down the Java process. However, with the -Xrs parameter, the JVM will minimize the use of these signals and instead rely on Java code to handle the shutdown operation. This can improve the stability of the JVM in certain situations.

  1. cmdExecute the command math-game-server.exe installto install it as a service (the service is not started at this time and the status is: stopped)
INFO  - Installing service 'Math-Game-Server (math-game-server)'...
INFO  - Service 'Math-Game-Server (math-game-server)' was installed successfully.
  1. Execute the command minio-server.exe startto start the service

Log printed after startup:

Insert image description here

Like the exe, only part of the test is done here:

Insert image description here

3. Summary

WinSW (Windows Service Wrapper) is an open source project that allows any executable file (usually .NET application, exe application, Java JAR file, etc.) to be converted into a Windows service. The goal of WinSW is to make it easier to run non-Windows services on Windows operating systems. It provides a way to wrap applications as Windows services, allowing you to start, stop, pause, and resume applications as services.

The following are the main features and uses of WinSW:

  1. Application packaging as services : WinSW allows you to wrap various types of applications as Windows services without modifying the application code. This is useful for deploying regular applications as services.
  2. Simplified management : Once an application is wrapped as a service, you can manage it using Windows Service Manager or command line tools such as sccommand. This makes deploying and managing applications on Windows more convenient.
  3. Custom configuration : WinSW allows you to customize the behavior of the service through XML or YAML configuration files, including service name, description, working directory, startup parameters, etc. This allows you to flexibly configure the service to suit different needs.

Guess you like

Origin blog.csdn.net/weixin_39168541/article/details/132854535