Linux set jar package to start automatically at boot (valid for personal test)

Foreword: key points key points

Please note that the export setting environment variables here are indispensable. If you don’t know the environment variables of the machine, you can view them through more /etc/profile .
insert image description here

The picture below is the jdk path I searched on the server. Using this path will not take effect

insert image description here

If the jdk-related information cannot be queried through more /etc/profile , obtain it through the following methods:

[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 May  8 17:36 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 71 May  8 17:36 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre/bin/java
[root@localhost ~]# cd /usr/lib/jvm
[root@localhost jvm]# ls
java-1.7.0-openjdk-1.7.0.191-2.6.15.5.el7.x86_64  jre        jre-1.7.0-openjdk                                jre-1.8.0          jre-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64
java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64     jre-1.7.0  jre-1.7.0-openjdk-1.7.0.191-2.6.15.5.el7.x86_64  jre-1.8.0-openjdk  jre-openjdk
[root@localhost jvm]# chmod +x /etc/rc.d/init.d/auto_jar.sh
[root@localhost jvm]# chmod 777 /etc/rc.d/rc.local
[root@localhost jvm]# reboot

  • Path to jdk:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar

script auto_jar.sh

#!/bin/bash

#配置jdk的路径 (使用more /etc/profile命令获取,直接copy)
export JAVA_HOME=/usr/java/jdk
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

nohup java -Dfile.encoding=utf-8 -jar /layman/bcdSystem/jar/jeecg-cloud-nacos-2.4.5.jar > /layman/bcdSystem/logs/nacosLog.txt 2>&1 &
sleep 10
nohup java -Dfile.encoding=utf-8 -jar /layman/bcdSystem/jar/jeecg-cloud-gateway-2.4.5.jar > /layman/bcdSystem/logs/getWayLog.txt 2>&1 &
nohup java -Dfile.encoding=utf-8 -jar /layman/bcdSystem/jar/jeecg-cloud-system-start-2.4.5.jar > /layman/bcdSystem/logs/systemLog.txt 2>&1 &
nohup java -Dfile.encoding=utf-8 -jar /layman/bcdSystem/jar/hl-bcd-flight-2.4.5.jar > /layman/bcdSystem/logs/flightLog.txt 2>&1 &
nohup java -Dfile.encoding=utf-8 -jar /layman/bcdSystem/jar/hl-bcd-data-2.4.5-exec.jar > /layman/bcdSystem/logs/dataLog.txt 2>&1 &

Be sure to set the format after completion (otherwise an error will be reported and will not succeed):
insert image description here

Put the script file under /etc/rc.d/init.d
insert image description here
and give the script permission :

chmod +x /etc/rc.d/init.d/auto_jar.sh        #脚本文件的全路径

The script grants boot-up self-starting permissions

vi /etc/rc.d/rc.local

Add the script address
insert image description here
to give the rc.local file execution permission :

chmod 777 /etc/rc.d/rc.local

restart server

reboot

Check

ps -ef | grep java

insert image description here

Guess you like

Origin blog.csdn.net/weixin_54514751/article/details/130427142