linux设置应用开机自启(通用:mysql、jar、nginx、solr...)

1. 业务场景

用于单机生产环境,防止服务器断电或者强制重启导致的服务下线。

2. 实现方案

  1. 对于无状态服务,可容器部署设置 restart: alwayssystemctl eable docker
  2. 对于有状态服务,可编写自启脚本,如下
  • ① 编写执行脚本
#!/bin/bash
APP_NAME=saveName
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20    # 等待应用启动的时间
APP_PORT=81 # 应用端口
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}  # 应用健康检查URL
APP_HOME=$(pwd) # 从package.tgz中解压出来的jar包放到这个目录下
HEALTH_CHECK_FILE_DIR=${APP_HOME}/status   # 脚本会在这个目录下生成nginx-status文件
JAR_NAME=${APP_HOME}/app.jar # jar包的名字
JAVA_OUT=/dev/null  #应用的启动日志
JAVA_HOME=${APP_HOME}/jdk-11.0.16
# 创建出相关目录
mkdir -p ${HEALTH_CHECK_FILE_DIR}
#mkdir -p ${APP_HOME}
usage() {
    echo "Usage: $PROG_NAME {start|stop|restart|init|jdk_install}"
    exit 2
}
jdk_install() {
	tar zxvf ${APP_HOME}/jdk-11.0.16_linux-x64_bin.tar.gz
    tee -a /etc/profile << EOF
	export JAVA_HOME=${APP_HOME}/jdk-11.0.16
	export PATH=${APP_HOME}/jdk-11.0.16/bin:\$PATH
	export CLASS_PATH=${APP_HOME}/jdk-11.0.16/lib
EOF
	echo "如没有jdk日志,请手动执行:source /etc/profile &java -version"
	source /etc/profile
	java -version
}
health_check() {
    exptime=0
    echo "checking ${HEALTH_CHECK_URL}"
    while true
        do
            status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code}  ${HEALTH_CHECK_URL}`
            if [ "$?" != "0" ]; then
               echo -n -e "\rapplication not started"
            else
                echo "code is $status_code"
                if [ "$status_code" == "401" ];then
                    break
                fi
            fi
            sleep 5
            ((exptime++))

            echo -e "\rWait app to pass health check: $exptime..."

            if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
                echo 'app start failed'
               exit 1
            fi
        done
    echo "check ${HEALTH_CHECK_URL} success"
}
start_application() {
    echo "starting java process"
    echo "nohup java -jar ${JAR_NAME} --spring.profiles.active=${env} > ${JAVA_OUT} 2>&1 &"
    nohup java -jar ${JAR_NAME} --spring.profiles.active=${env} > ${JAVA_OUT} 2>&1 &
    echo "started java process"
}

stop_application() {
   checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`

   if [[ ! $checkjavapid ]];then
      echo -e "\rno java process"
      return
   fi

   echo "stop java process"
   times=60
   for e in $(seq 60)
   do
        sleep 1
        COSTTIME=$(($times - $e ))
        checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
        if [[ $checkjavapid ]];then
            kill -9 $checkjavapid
            echo -e  "\r        -- stopping java lasts `expr $COSTTIME` seconds."
        else
            echo -e "\rjava process has exited"
            break;
        fi
   done
   echo ""
}
start() {
    start_application
    health_check
}
stop() {
    stop_application
}
case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    jdk_install)
        jdk_install
    ;;
    *)
        usage
    ;;
esac

  • ① 编写自启脚本
#!/bin/sh
# chkconfig: 2345 20 20
# clear file
base=baseArg
serveStart() {
  echo "正在启动Solr"
  cd baseArg/serve
  sh baseArg/serve/deploy.sh start
}
serveStart
  • ② 注册自启脚本至linux开启自动调用目录
ACTION=$1
usage() {
    echo "Usage: $PROG_NAME {init|stopAll}"
    exit 2
}
stopAll(){
checkjavapid=`ps -ef |  grep $serveName| awk '{print$2}'`
kill -9 $checkjavapid
}
init(){
base=$(pwd)
sed -i "s|baseArg|${base}|g" $base/autoSave.sh
chmod 777 $base/autoSave.sh
rm -rf /etc/init.d/autoSave.sh
cp -rf $base/autoSave.sh /etc/init.d/autoSave.sh
chmod 777 /etc/init.d/autoSave.sh
chkconfig --add autoSave.sh
chkconfig autoSave.sh on
chkconfig --list
}
case "$ACTION" in
    init)
        init
    ;;
    stopAll)
        stopAll
    ;;
    *)
        usage
    ;;
esac



你可能感兴趣的:(linux,mysql,jar)