一.supervisor介紹
supervisor是一個進程管理的python系統,可以方便的監控,管理linux系統中運行的進程服務
supervisor主要包括supervisord和supervisorctl
supervisord是supervisor的服務端,它負責自行調用啓動子程序,響應來自客戶端的命令,重新啓動崩潰或退出的子進程,記錄其子進程stdout和stderr 輸出以及生成和處理與子進程生存期中的點相對應的“事件”。
supervisorctl是supervisor的命令行客戶端(command-line client),提供類似shell的命令 交互功能。supervisorctl通過使用UNIX域套接字或Internet(TCP)套接字與supervisord服務端進行通信
二.supervisor安裝與配置
supervisor安裝:
yum -y install supervisor
supervisor配置:
配置的話可以再supervisord.conf文件中配置,也可以設置子配置文件如下:
/etc/supervisord.conf:
# 這裏是修改後的,原來是*ini,我這改成了*.conf
[include]
files = supervisord.d/*.conf
/etc/supervisord.d/igolf.conf:
這些配置的具體含義可參考如下:
https://www.cnblogs.com/549294286/p/9229868.html
https://www.jianshu.com/p/0b9054b33db3
[program:service-act]
directory = /home/fancygolf
#command = /bin/bash -c 'sleep 5s && source "$0" && exec "$@"' /etc/profile java -jar service-act.jar
command = sh -x supervisor_deploy.sh restart service-act.jar
autostart = true
startsecs = 20
autorestart = true
startretries = 10
user = zeliang
priority = 1100
redirect_stderr = true
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups = 10
# stdout 日誌文件,需要注意當指定目錄不存在時無法正常啓動,所以需要手動創建目錄(supervisord 會自動創建日誌文件)
stdout_logfile = /home/fancygolf/supervisor_logs/service-act.log
suervisor_deploy.sh:
參考即可,根據實際情況調整
#!/bin/bash
APP_NAME=$2
#使用說明,用來提示輸入參數
usage() {
echo "Usage: ./xxx.sh [start|stop|restart|status] xxx.jar"
exit 1
}
#檢查程序是否在運行
is_exist() {
pid=`ps -ef | grep $APP_NAME | grep -v grep | grep -v sh | awk '{print $2}'`
#grep -v sh 是過濾掉本腳本產生的PID (執行的時候 sh....jar會產生)
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#檢查基礎服務是否運行
is_serve_running(){
value=`curl -s http://localhost:8888/redis-test.yml`
if [ ! $value ];then
return 0
else
return 1
fi
}
#啓動
start() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
is_serve_running
if [ $? -eq "0" ]; then
echo "congfig serve is not running"
exit 1
else
source /etc/profile
java -jar $APP_NAME
echo -e "\033[31m $APP_NAME IS RUNNING SUCCESSFULLY \033[0m"
#tail -f -n 100 ./catalina.out
fi
fi
}
#停止
stop() {
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
echo -e "\033[31m $APP_NAME IS STOPPING SUCCESSFULLY \033[0m"
else
echo "${APP_NAME} is not running"
fi
}
#運行狀態
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重啓
restart() {
stop
start
}
#根據$1參數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
激活配置:
# 執行一次即可
supervisord -c supervisord.conf
# 每次修改配置執行一次
supervisorctl reload
測試:
kill springboot進程 或者 重啓reboot服務器測試效果