In the windows environment, use winsw to register the jar package as a service (realize boot-up self-start and configure log output mode)

foreword

If the Windows system uses the java -jar m command line to run a Java project, a black window will pop up. First of all, it is easy to miss the point and cause the program to close, and secondly, we hope to be able to start automatically when the Windows system is turned on. Therefore, for the SpringBoot program, the current mainstream method is to use winsw, which is simple and easy to configure.

1. Download the winsw tool

https://github.com/winsw/winsw/releases

insert image description here

2. File configuration

Modify WinSW and sample-minimal.xml name

Put the downloaded file and the jar file together, and change the two file names to 服务名(custom service name)
insert image description here

edit xml file

Note 不要出现中文that the jar file address in the arguments and logpath tags should be changed to its own directory

<service>
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>testapp</id>
  <!-- Display name of the service -->
  <name>test service</name>
  <!-- Service description -->
  <description>This service is a service created from a minimal configuration</description>
   
  <env name="JAVA_HOME" value="%JAVA_HOME%"/>
     
  <executable>java</executable>
 
  <arguments>-jar "H:\testautostart\cxstar-api-rest-1.0.0.jar"</arguments>
 
  <startmode>Automatic</startmode>

  <logpath>H:\testautostart\logs</logpath>
 
  <logmode>rotate</logmode>
	
</service>

logmode: log output mode, the default is append, official document

Parameter analysis

tag name effect
id Service ID, which must be a unique system identifier in the windows system
name Service display name, cannot use Chinese, only English, numbers, horizontal lines, etc.
description Service description, describing what the service does
env set environment variables
executable The path to start the executable file. If the Java environment variable is configured, you can directly use java instead. Otherwise 使用全路径(it's easy to get stuck here)
arguments Arguments passed to the executable
start mode Specifies the startup mode of the Windows service, it can be one of the following values: boot, system, automatic or manual, the default value is "Automatic"
logpath Configure the log path
logging mode Log output mode, the default is append

log mode:

  1. append (append mode) is characterized by outputting all log files in one file, and this file may become larger and larger
  2. rotate (rotation mode, recommended) when the log file size reaches 10 megabytes (default value), winsw will re-output the log to another log file, and keep up to 8 by default
  3. reset (reset mode) resets the log file every time the service is restarted
  4. none (ignore pattern) generates almost no log files

3. Registration service

Enter cmd in the current directory to enter the command line mode and execute the service registration command

testapp.exe install

insert image description here
The service already exists in the service,
insert image description here
double-click to set it to automatic
insert image description here

Uninstall service:

testapp.exe uninstall

insert image description here
Other service commands:

testapp.exe start:启动服务
testapp.exe stop:停⽌服务
testapp.exe restart:重启服务
testapp.exe status:输出当前服务的状态

About the problem of failure to start in windows server 2008

After the service starts successfully, it is automatically closed, and the log file is not generated. Execute the java -jar command, and the service can be started normally. In Control Panel—Administrative Tools—Event Viewer—Window Log—Application—find the log of the corresponding service. as follows:

Service cannot be started. 
System.ComponentModel.Win32Exception: The system cannot find the file specified 
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 
at winsw.WrapperService.StartProcess(Process processToStart, String arguments, String executable) 
at winsw.WrapperService.OnStart(String[] _) 
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

The prompt is very clear, the system did not find the specified file, but the executable has been configured in the winsw xml file, and the environment variable configuration is correct. Then why is it still saying that the file is not found? So I changed java to the full path, re-registered the service and successfully started the service

Guess you like

Origin blog.csdn.net/qq_29864051/article/details/130937886