您现在的位置是:首页 >技术教程 >Linux守护进程启动进程健康监测进程网站首页技术教程
Linux守护进程启动进程健康监测进程
简介Linux守护进程启动进程健康监测进程
#!/bin/bash
# 应用名
APP_NAME=smart_school-boot
# 应用端口
APP_PORT=17206
# 应用健康检查URL
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/smart-school/doc.html
# jar所在的根目录
APP_HOME=/home/edu/edupkg/jar/${APP_NAME}
# jar的绝对路径
JARNAME=jeecg-boot-module-system-3.2.0.jar
JAR_NAME=${APP_HOME}/${JARNAME}
#应用的启动日志
JAVA_OUT=${APP_HOME}/logs/start_${APP_NAME}_$(date "+%Y-%m-%d-%H").log
echo logfile=$JAVA_OUT
#
PID_OUT==${APP_HOME}/${APP_NAME}_${APP_PORT}_pid
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20 # 等待应用启动的时间
# 创建出相关目录
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
echo "Usage: $PROG_NAME { start | stop | restart | status | pid | health | guard | help }"
exit 2
}
process_check(){
P_PID=-1
pid=$(ps -ef | grep ${APP_NAME} | grep ${JARNAME} | grep -v grep | grep -v crontab | grep -v monitor | awk '{print $2}')
echo "${APP_NAME} ${JARNAME} pid is [${pid}]"
if [ ! -n "$pid" ]; then
#echo $pid
echo "查找" ${APP_NAME} ${JARNAME} "进程没有找到!!!返回-1"
pid=-1
fi
#echo $pid
P_PID=$pid
P_PID_COUNT=0
P_PID_COUNT=$(ps -ef | grep ${APP_NAME} | grep ${JARNAME} | grep -v grep | grep -v crontab | grep -v monitor | awk '{print $2}'| wc -l)
echo "${APP_NAME} ${JARNAME} pid count = [${P_PID_COUNT}]"
if [ $P_PID_COUNT -eq 0 ]; then
echo "查找" ${APP_NAME} ${JARNAME} "进程数量为"$P_PID_COUNT"!!!"
fi
}
# 进程守卫
process_guard(){
process_check
if [ $P_PID_COUNT -eq 0 ]; then
echo "查找" ${APP_NAME} ${JARNAME} "进程数量为"$P_PID_COUNT"!!!,现在立即启动进程。。。请稍后。。。"
start
sleep 5
echo "启动完毕,再次检查进程。。。"
process_check
fi
if [ $P_PID_COUNT -gt 0 ]; then
echo "running 进程运行中,无需再次启动. pid=${P_PID} pid_count=${P_PID_COUNT}"
fi
}
#健康检查
health_check() {
#sleep 60
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 "
application not started"
else
echo "code is $status_code"
if [ "$status_code" == "200" ];then
break
fi
fi
sleep 1
((exptime++))
echo -e "
Wait app to pass health check: $exptime..."
if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
echo '[ERROR] [failed] app start failed'
exit 1
fi
done
echo "[OK] [success] check ${HEALTH_CHECK_URL} success"
}
#启动应用
start_application() {
echo "starting java process"
nohup java -jar ${JAR_NAME} > ${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 "
no 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 "
-- stopping java lasts `expr $COSTTIME` seconds."
else
echo -e "
java process has exited"
break;
fi
done
echo ""
}
#启动
start() {
start_application
echo -e "please wait...health checking..."
#sleep 45
#health_check
process_check
}
#停止
stop() {
stop_application
process_check
}
#命令分流
case "$ACTION" in
status)
health_check
;;
pid)
process_check
;;
guard)
process_guard
;;
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
health)
health_check
;;
help)
usage
;;
*)
usage
;;
esac
文件名:start.sh
参数说明:
# 应用名(查进程关键字): APP_NAME=smart_school-boot
# 应用端口(健康监测用): APP_PORT=17206
# 应用健康检查URL(健康检查):HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/smart-school/doc.html
# jar的绝对路径(进程检查):JARNAME=jeecg-boot-module-system-3.2.0.jar
使用方法:
启动: ./start.sh start
停止: ./start.sh stop
重启:./start.sh restart
健康监测:./start.sh health
进程监测:./start.sh pid
进程守卫(守护):./start.sh guard
帮助:./start.sh help
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。