The bat file is registered as a Windows service with dependency settings

1. Batch file registration as a service

1.1 Application scenarios

The jar package under the microservice architecture is usually started through the command line java -jar -Dfile.encoding=utf-8 %JAVA_OPTS% gateway.jar. The problem is that a console window will appear, and in order to ensure that the service is available, the console window must be opened all the time. In addition, the service needs to be manually started every time it is turned on. This is not very user friendly.

You can write commands into bata batch file, and then register the batch file as a Windows service, and set it to start automatically at boot. In this way, the service can be started automatically at startup, and at the same time, there is no console window to prevent users from accidentally closing it.

1.2 Operation steps

1.2.1 Write batch files;

Example: Run_Gateway.bat

@echo off
echo.
echo [INFO] Startup gateway server
echo.

set JAVA_OPTS=-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m

java -jar -Dfile.encoding=utf-8 %JAVA_OPTS% gateway.jar

pause

1.2.2 Register the batch file as a service with instsrv+srvany;

For instsrv and srvany, please refer to the bottom link 1 .

  1. Copy instsrv.exe and srvany.exe to C:\Toolsthe directory
  2. Run cmd as an administrator and switch to the C:\Tools directory
  3. Run the command:instsrv MyService C:\Tools\srvany.exe

Note:Myservice It is the name of the customized service, which can be changed arbitrarily according to the application name

Run successfully!

configuration

  1. Open the registry: (enter in cmd: regedit)
  2. ctrl+F, search Myservice(previously customized service name)
  3. Right-click Myserviceto create a new item namedParameters
  4. Then create a few new ones in Parameters字符串值
  • Name Application Value: The address of the program you want to run as a service.
  • Name AppDirectory Value: The path to the folder where the program you want to run as a service is located.
  • Name AppParameters Value: The parameters you need to start the program that you want to run as a service.

Then start the service Myserviceto run the bat file in the background

1.2.3 Set dependencies between services;

Refer to Chapter 2 to configure dependencies between services

1.2.4 Start Test

After the setting is complete, you can manually start it once in the service manager, and then it will start automatically every time the computer is turned on.

2. Configure dependencies between services

2.1 Application scenarios

There are dependencies between some services, or there is a sequential startup sequence, such as services Run_Gatewaymust be started after the database service MySQLis started. At this time, you need to Run_Gatewayadd dependency configuration to the service.

2.2 Operation steps

  1. run cmd as administrator;
  2. Excuting an order
sc config "Run_Gateway" depend= "MySQL"

Indicates that the Run_Gateway service depends on the MySQL service.

reference link

Guess you like

Origin blog.csdn.net/wml00000/article/details/123181368