Background:
We are running java springboot applications on Centos8 (for now...) as service but applications take some time to startup: flyway, DAO initialization... Actualy start of service can take about 7 minuts
SystemD service
[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
Application start script
#!/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
Questions
- Is there any way to monitor starting java application to see in which state currently is (loading, started, restarting, stale...)?
- Any hints for above service/start scripts?
Thanks!