Centos8 Java 应用程序即服务

Centos8 Java 应用程序即服务

背景:

我们正在 Centos8 上运行 java springboot 应用程序(目前......)作为服务,但应用程序需要一些时间才能启动:flyway、DAO 初始化......实际上服务启动大约需要 7 分钟

SystemD 服务

[Unit]
Description=SSO
After=syslog.target network.target

[Service]
Type=forking
User=my_app
Group=my_app

SuccessExitStatus=143

ExecStart=/bin/bash -c "exec /home/my_app/sso/run.sh start >&1"
ExecStop=/bin/bash -c "exec /home/my_app/sso/run.sh stop >&1"
ExecReload=/bin/bash -c "exec /home/my_app/sso/run.sh restart >&1"

[Install]
WantedBy=multi-user.target

应用程序启动脚本

#!/bin/bash
BASE_DIR=/home/my_app/sso/
LOG_PATH=$BASE_DIR/logs/
PID_FILE=$BASE_DIR/app.pid

JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
... and tons of another JAVA_OPTS ...

START_COMMAND="java $JAVA_OPTS -jar $BASE_DIR/deploy/sso.jar"

start() {
  PID=`$START_COMMAND >> $LOG_PATH/sso-${HOSTNAME}.log & echo $!`
}

case "$1" in
start)
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            start
        else
            echo "Already running [$PID]"
            exit 0
        fi
    else
        start
    fi

    if [ -z $PID ]; then
        echo "Failed starting"
        exit 1
    else
        echo $PID > $PID_FILE
        echo "Started [$PID]"
        exit 0
    fi
;;

status)
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            echo "Not running (process dead but PID file exists)"
            exit 1
        else
            echo "Running [$PID]"
            exit 0
        fi
    else
        echo "Not running"
        exit 0
    fi
;;
stop)
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            echo "Not running (process dead but PID file exists)"
            rm -f $PID_FILE
            exit 1
        else
            PID=`cat $PID_FILE`
            kill -term $PID
            echo "Stopped [$PID]"
            rm -f $PID_FILE
            exit 0
        fi
    else
        echo "Not running (PID not found)"
        exit 0
    fi
;;
restart)
    $0 stop
    $0 start
;;
*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 0
esac

问题

  1. 有没有什么方法可以监视正在启动的 Java 应用程序以查看当前处于哪种状态(加载、启动、重新启动、过期……)?
  2. 对上述服务/启动脚本有什么提示吗?

谢谢!

相关内容