2022.11.15 Centos7设置Jar包开机自启动
一、创建自启脚本 auto_start.sh
在 /etc/rc.d/init.d 目录下创建 auto_start.sh 文件
#!/bin/bash
#配置jdk的路径
export JAVA_HOME=/usr/jdk1.8.0_144
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
#项目1
export APP_NAME1="XXXX.jar"
export JVM_OPTS1="-Dname=${APP_NAME1} -Duser.timezone=Asia/Shanghai -Xms512M -Xmx2g -XX:PermSize=256M -XX:MaxPermSize=2g -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
#项目2
export APP_NAME2="XXXX.jar"
export JVM_OPTS2="-Dname=${APP_NAME2} -Duser.timezone=Asia/Shanghai -Xms512M -Xmx2g -XX:PermSize=256M -XX:MaxPermSize=2g -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"
#静默启动
nohup java -jar ${JVM_OPTS1} /usr/local/springboot/${APP_NAME1} > /dev/null 2>&1 &
nohup java -jar ${JVM_OPTS2} /usr/local/springboot/${APP_NAME2} > /dev/null 2>&1 &
备注:
- XXXX.jar需替换为实际的jar包
- 若不清楚JDK位置,可用命令查询:
echo $JAVA_HOME
赋予执行权限
chmod +x /etc/rc.d/init.d/auto_start.sh
二、设置开机自启动
编辑/etc/rc.d目录下的rc.local文件
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
#新增
/etc/rc.d/init.d/auto_start.sh
备注:
- 若需延迟启动,则可以加上 sleep
#延时5分钟
sleep 5m
#执行
/etc/rc.d/init.d/auto_start.sh
赋予rc.local文件执行权限
chmod +x /etc/rc.d/rc.local
三、重启服务器
重启命令:
reboot
查看包含.jar后缀的文件启动状态
ps -ef | grep .jar
作者:薛定谔的猴子
链接:https://www.jianshu.com/p/021330e1f200
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。