一.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服务器测试效果