Supervisor 是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启。
两个重要的组件:
supervisord: Supervisor的服务端程序,使用前,需要先启动该组件
supervisorctl: Supervisor的客户端程序,用来实际控制子进程(自定义的服务、程序)
猪: supervisord是主进程,supervisorctl是给守护进程发送命令的客户端工具。
安装一个试一下吧!
yum -y install python-setuptools
easy_install supervisor
可以使用supervisord -h查看是否成功。
[root@localhost /]# supervisord -h
supervisord -- run a set of applications as daemons.
Usage: /usr/local/bin/supervisord [options]
Options:
-c/--configuration FILENAME -- configuration file path (searches if not given)
-n/--nodaemon -- run in the foreground (same as 'nodaemon=true' in config file)
-h/--help -- print this usage message and exit
-v/--version -- print supervisord version number and exit
-u/--user USER -- run supervisord as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-d/--directory DIRECTORY -- directory to chdir to when daemonized
-l/--logfile FILENAME -- use FILENAME as logfile path
-y/--logfile_maxbytes BYTES -- use BYTES to limit the max size of logfile
-z/--logfile_backups NUM -- number of backups to keep when max bytes reached
-e/--loglevel LEVEL -- use LEVEL as log level (debug,info,warn,error,critical)
-j/--pidfile FILENAME -- write a pid file for the daemon process to FILENAME
-i/--identifier STR -- identifier used for this instance of supervisord
-q/--childlogdir DIRECTORY -- the log directory for child process logs
-k/--nocleanup -- prevent the process from performing cleanup (removal of
old automatic child log files) at startup.
-a/--minfds NUM -- the minimum number of file descriptors for start success
-t/--strip_ansi -- strip ansi escape codes from process output
--minprocs NUM -- the minimum number of processes available for start success
--profile_options OPTIONS -- run supervisord under profiler and output
results based on OPTIONS, which is a comma-sep'd
list of 'cumulative', 'calls', and/or 'callers',
e.g. 'cumulative,callers')
新建一个supervisor文件
[root@localhost /]# mkdir /etc/supervisor/
[root@localhost /]# cd /etc/supervisor/
拷贝supervisored.conf(默认配置文件)到新建的supervisor文件,再添加一个conf.d文件夹存放子进程的配置文件
[root@localhost supervisor]# ls
conf.d supervisored.conf
修改supervisored.conf里面include配置,去除注释,添加files
[include]
files = conf.d/*.conf
[root@localhost supervisor]# cd conf.d/
子进程配置文件
一般放在:/etc/supervisor/conf.d/目录 。一个脚本对应一个配置文件。
配置说明:
;*为必须填写项
;*[program:应用名称]
[program:zby]
;执行目录,若有/home/supervisor_test/test1.py
;将directory设置成/home/supervisor_test
;则command只需设置成python test1.py
;否则command必须设置成绝对执行目录
directory=/opt/zby/zby/
;*命令路径,如果使用python启动的程序应该为 python /home/test.py,
;不建议放入/home/user/, 对于非user用户一般情况下是不能访问
command = /opt/zby/venv/bin/gunicorn -c gunicorn_config.py zby_app:app
numprocs=1 ;进程数量
;当numprocs为1时,process_name=%(program_name)s
;当numprocs>=2时,%(program_name)s_%(process_num)02d
process_name=%(program_name)s
user=root ;以root用户执行
environment=zby_env=dev ;环境变量设置
autostart=true ;在supervisord 启动的时候也自己启动
startsecs = 5 ;启动5秒后没有异常退出,就当做已经正常启动
autorestart=true ;程序异常自动重启
startretries = 3 ;启动失败自动重试次数 默认是3
stdout_log_file_backups = 100; 日志文件备份数
stdout_logfile = /var/zby/stdout.log
stderr_logfile = /var/zby/stderr.log
启动supervisorctl
supervisorctl -c /etc/supervisor/supervisored.conf
supervisor相关命令
supervisor> help
default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
日志目录:tail -f /tmp/supervisord.log