Monitor jar package startup and automatic execution in Linux

                                                     #Linux中监控jar包的启动

Description:
Creator: csc
Time: 2023-06-05
Environment: OpenCloudOS/CentOS 8.
Personal test. Whether you restart the system or kill the process with kill -9, it will start automatically.

You can write a systemd service to allow the system to automatically start the jar package. Here are the specific steps:

Step 1. First enter the /etc/systemd/system/ directory in the Linux system, and the command is as follows

 cd /etc/systemd/system 

Step 2. The command to create a myapp.service file is as follows:
#Friendly reminder: If the file does not exist, you can also vim directly. The myapp.service command will create it directly and enter the editing mode. I will not explain it below.

touch myapp.service
#友情提示 如果文件不存在也可以直接vim myapp.service命令 会直接创建进入编辑模式
vim myapp.service

Step 3. Edit the file command as follows:

vim myapp.service

Copy the following command into the myapp.service command:

[Unit]
Description=My App Service
After=network.target

[Service]
User=root
WorkingDirectory=/path/to/your/jar/file/
ExecStart=/usr/bin/java -jar your.jar
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Property explanation:

   Description表示服务的描述,可以自定义;
   After表示该服务要在网络服务启动后才能启动,也可以指定某个jar包启动之后再启动该jar包服务 
   User表示该服务要以root用户身份运行;
   WorkingDirectory表示你自己jar包所在的路径;
   **ExecStart表示要执行的命令 /usr/bin/java 是你jdk安装的目录下的bin文件夹 别忘了改成你自己的;**
   RestartRestartSec表示服务停止后要重新启动的时间间隔

the fourth step:

 点击 esc
 输入  :wq 退出编辑
  1. After exiting the file, then execute the following command:
systemctl daemon-reload
systemctl enable myapp.service
systemctl start myapp.service
#查看运行状态
systemctl status myapp.service
#停止服务
systemctl stop myapp.service

In this way, the system will automatically start your jar package.

Extension:
4. The above is actually enough. But you can also specify the stack size and environment for running:
4.1 First "edit and create an .env file" in the same directory as the jar. This file must have executable permissions as shown below:

  vim netty-server.env  #netty-server文件的名字 你们自己随意

After success,
Insert image description here
the commands in the file shown in Figure 4.2 are as follows:

   JAVA_OPTS=-Xms256m -Xmx512m -Xmn256m -Xss512k

Insert image description here

  1. Then add in [service] in the my.service file:
EnvironmentFile=-/data/netty-server.env
  1. The complete command for my my.service file is as follows:
[Unit]
Description=My App Service
After=network.target

[Service]
User=root
EnvironmentFile=-/data/netty-server.env
WorkingDirectory=/data
ExecStart=/usr/local/java/jdk1.8.0_361/jre/bin/java $JAVA_OPTS -jar /data/netty-server.jar
SuccessExitStatus=143
StandardOutput=journal
StandardError=journal
Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

Then just execute it in sequence:

systemctl daemon-reload
systemctl enable myapp.service
systemctl start myapp.service

Guess you like

Origin blog.csdn.net/qq_37120477/article/details/131047103