在linux服务器下,出现服务器断电、服务器重启等情况,这时候就需要一些服务开机自启动。
环境:centos7虚拟机、jar包、jdk8
前提:
1、centos7已经配置jdk8环境
2、上传jar包、新建脚本文件、日志文件等
2.1、新建启动脚本 jar-cachfile-service-start.sh
#!/bin/sh
rt JAVA_HOME=/etc/alternatives/jre_1.8.0_openjdk
export PATH=$JAVA_HOME/bin:$PATH
nohup java -jar /LazmProject/cachfile_jar/cachfile.jar --spring.config.location=/LazmProject/conf/cachfile.properties >/LazmProject/logs/cachfile.log &
echo $! > /data/jarservice/db-cachfile-service.pid
2.2、新建停止脚本jar-cachfile-service-stop.sh
#!/bin/sh
PID=$(cat /data/jarservice/db-cachfile-service.pid)
kill -9 $PID
3、增加脚本执行权限
chmod +x jar-cachfile-service-start.sh
chmod +x jar-cachfile-service-stop.sh
4、启动脚本
#启动服务
./data/jarservice/jar-cachfile-service-start.sh
#停止服务
./data/jarservice/jar-cachfile-service-stop.sh
5、添加开机自动启动
vi /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
/data/jarservice/jar-cachfile-service-start.sh
6、设置rc.local 可执行权限
centos7下rc.local的权限被降低,需要手动为其赋予可执行权限
chmod u+x /etc/rc.d/rc.local
7、reboot重启下服务器看下结果,是否自动启动了jar包