2024-01-04
服务器
supervisor
760
一、安装 supervisor
1.安装 pip
yum install python-pip
pip install --upgrade pip # 升级 pip
pip -V
2.安装 supervisor
# supervisor 只能在 python2 环境中安装,不支持 python3,supervisorctl 服务客户端,supervisord 后台监控服务
pip install supervisor
supervisord -v
二、supervisor 设置
1.新建文件
vim /etc/init.d/supervisor
开机文件内容:
#!/bin/bash
#
# supervisord Startup script for the Supervisor process control system
#
# Author: Mike McGrath (based off yumupdatesd)
# Jason Koppe adjusted to read sysconfig,
# use supervisord tools to start/stop, conditionally wait
# for child processes to shutdown, and startup later
# Erwan Queffelec
# make script LSB-compliant
# Greg Smethells
#. Allow supervisorctl to be overridden
#
# chkconfig: 345 83 04
# description: Supervisor is a client/server system that allows \
# its users to monitor and control a number of processes on \
# UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
# its users to monitor and control a number of processes on
# UNIX-like operating systems.
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
. /etc/sysconfig/supervisord
fi
# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=${SUPERVISORCTL-/usr/bin/supervisorctl}
supervisord=${SUPERVISORD-/usr/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/var/run/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
sockfile=${SOCKFILE-/var/run/supervisord.sock}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0
start() {
echo -n $"Starting $prog: "
daemon --pidfile=${pidfile} $supervisord $OPTIONS
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
touch ${lockfile}
$supervisorctl $OPTIONS status
fi
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile} ${sockfile}
}
reload() {
echo -n $"Reloading $prog: "
LSB=1 killproc -p $pidfile $supervisord -HUP
RETVAL=$?
echo
if [ $RETVAL -eq 7 ]; then
failure $"$prog reload"
else
$supervisorctl $OPTIONS status
fi
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $supervisord
RETVAL=$?
[ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
;;
restart)
restart
;;
condrestart|try-restart)
if status -p ${pidfile} $supervisord >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
RETVAL=2
esac
exit $RETVAL
2.开机启动服务
chmod 777 /etc/init.d/supervisor # 设置权限
# 配置成开机启动服务
chkconfig --add supervisor
chkconfig supervisor on
chkconfig --list | grep "supervisor" # 查看状态
supervisor 0:off 1:off 2:on 3:on 4:on 5:on 6:off
3.生成主配置文件
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
三、文件配置
1.主进程配置文件
vim /etc/supervisor/supervisord.conf
cat /etc/supervisor/supervisord.conf |grep ^[^\;]
# 修改 [inet_http_server] 模块,去掉前面的分号(关闭注释)
[inet_http_server] ; inet (TCP) server disabled by default
port=127.0.0.1:9581 ; ip_address:port specifier, *:port for all iface
username=jdzor ; default is no username (open server)
password=123456 ; default is no password (open server)
# 修改 [supervisorctl] 模块
[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://127.0.0.1:9581 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
prompt=jdzor-supervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
# 修改 [include] 模块
[include]
files = /etc/supervisor/*.ini
2. 监控进程配置文件
# 项目中开启 workerman 长驻进程监控
vim /etc/supervisor/xxx.gateway.ini
# xxx.subscribe.ini 文件配置:
[program:jdzor-electron.subscribe] ; 服务名,例如work
command=php artisan gateway-worker start ; 带有参数的可执行命令
;process_name=%(process_num)s ; 进程名,当numprocs>1时,需包含%(process_num)s
;numprocs=2 ; 启动进程的数目数
directory=/www/web/stkj-software-php ; 运行前切换到该目录
;umask=022 ; 进程掩码
;priority=999 ; 子进程启动关闭优先级
autostart=true ; 子进程是否被自动启动
startsecs=5 ; 成功启动几秒后则认为成功启动
startretries=3 ; 子进程启动失败后,最大尝试启动的次数
autorestart=unexpected ; 子进程意外退出后自动重启的选项,false, unexpected, true。unexpected表示不在exitcodes列表时重启
exitcodes=0,2 ; 期待的子程序退出码
;stopsignal=QUIT ; 进程停止信号,可以为TERM,HUP,INT,QUIT,KILL,USR1,or USR2等信号,默认为TERM
;stopwaitsecs=10 ; 发送停止信号后等待的最大时间
;stopasgroup=false ; 是否向子进程组发送停止信号
;killasgroup=false ; 是否向子进程组发送kill信号
redirect_stderr=true ; 是否重定向日志到标准输出
stdout_logfile=/www/log/php/electron.subscribe.log ;进程的stdout的日志路径
;stdout_logfile_maxbytes=1MB ; 日志文件最大大小
;stdout_logfile_backups=10
;stdout_capture_maxbytes=1MB
;stderr_logfile=/a/path ; stderr的日志路径
;stderr_logfile_maxbytes=1MB
;stderr_logfile_backups=10
;stderr_capture_maxbytes=1MB
;environment=A="1",B="2" ; 子进程的环境变量
;serverurl=AUTO ; 子进程的环境变量SUPERVISOR_SERVER_URL
四、后台服务端、命令终端
1.启动 supervisord 守护服务
supervisord -c /etc/supervisor/supervisord.conf
# 常用的命令参数说明:
# -c:指定配置文件路径
# -n:是否非守护态运行
# -l:日志文件目录
# -i:唯一标识
2.命令终端
supervisorctl
Server requires authentication
Username:jdzor
Password:
jdzor-electron.subscribe RUNNING pid 17615, uptime 71 days, 2:22:12
3.supervisorctl 常用命令
status xxx # 查看服务状态
update xxx # 重新加载配置文件
restart xxx # 重新启动服务
stop xxx # 停止服务
pid xxx # 查看某服务的 pid
tail xxx # 输出最新的 log 信息
shutdown # 关闭 supervisord 服务
# supervisor 监控的进程不能开启 daemon 模式
标签:
supervisor