做了java项目,jar包启动脚本已经写好了。想让他开机自启,做下记录:
1. 要自启的脚本
脚本位置任意,就叫 app_all_start.sh
吧。暂放root目录下吧。脚本要有权限哦。
#!/bin/bash
#chkconfig: 35 20 80
#description: jar start
export JAVA_HOME=/usr/local/jdk1.8.0_381
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
echo "--------wait for mysql、redis、nacos--------------"
sleep 180s
## 多个jar包都要启动
/opt/workspace/test1/start.sh
sleep 20s
/opt/workspace/test2/start.sh
sleep 20s
/opt/workspace/test3/start.sh
sleep 20s
/opt/workspace/test4/start.sh
2. 单个jar 包 启动文件
就是上面那个start.sh
文件
#!/bin/bash
export JAVA_HOME=/usr/local/jdk1.8.0_381
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
nohup java -jar -Xmx2g -Xms2g -Dfile.encoding=utf-8 -Dspring.config.location=classpath:/application-test.yml /opt/workspace/test1/test1.jar >/dev/null 2>&1 &
3. app_all_start.sh
自启
- 修改
rc.local
文件
vim /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
/root/app_all_start.sh
- 给
rc.local
添加执行权限
chmod +x /etc/rc.d/rc.local
- 重启服务器测试
网友评论