进程守护工具(Supervisor)
Supervisor是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。
它可以很方便的监听、启动、停止、重启一个或多个进程。
用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
安装
sudo apt-get install supervisor
# 不推荐如下安装命令 (需要手动创建配置文件、设置开机启动)
pip install supervisor
结构
supervisord- server 端supervisorctl- client 端
supervisord 配置
# 打开配置文件 (这个文件基本不修改, 如果要修改可参考文章后面的配置项)
sudo vim /etc/supervisor/supervisord.conf
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
配置文件可添加如下代码( 通过web可以查看管理的进程 ) 启动服务后通过 lsof -i:9001 命令查看是否监听, 访问 http://ip:9001即可访问
[inet_http_server]
port=9001 ; 端口
username=user ; 账号
password=123456
添加自定义 program 配置块 (可参考文章后面的配置项)
# 进入自定义文件夹
cd /etc/supervisor/conf.d/
# 创建配置文件 (我们以blog_app为例子)
sudo vim blog_app.conf
写入如下内容
[program:blog_app_service] ; 程序名称,在 supervisorctl 中通过这个值来对程序进行一系列的操作
command=python /home/app/blog_app_server.py ; 启动命令,与手动在命令行启动的命令是一样的
autorestart=True ; 程序异常退出后自动重启
autostart=True ; 在 supervisord 启动的时候也自动启动
redirect_stderr=True ; 把 stderr 重定向到 stdout,默认 false
user=root ; 用哪个用户启动
directory=/home/app/ ; 程序的启动目录
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 10 ; stdout 日志文件备份数
stdout_logfile = /data/logs/supervisor/blog_app_server.log ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
相关命令
服务启动
sudo supervisord -c /etc/supervisod.conf
客户端命令
# 查看所有进程的状态
supervisorctl status
# 启动某个进程 blog_app_service 为 [program:blog_app_service]里配置的值
supervisorctl start blog_app_service
# 停止某个进程
supervisorctl stop blog_app_service
# 停止全部
supervisorctl stop all
# 重启某个进程
supervisorctl restart blog_app_service
# 更新新的配置到supervisord服务, 不会重启原来已运行的程序
supervisorctl update
# 载入所有配置文件,并按新的配置启动、管理所有进程(会重启原来已运行的程序)
supervisorctl reload
参数详解
supervisord 服务端
logfile: log文件路径logfile_maxbytes: log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小logfile_backups: 轮转日志备份的数量,默认是10,如果设置为0,则不备份loglevel: error、warn、info、debug、trace、blather、criticalpidfile: pid文件路径umask: umask值,默认022nodaemon: 如果设置为true,则supervisord在前台启动,而不是以守护进程启动minfds: supervisord在成功启动前可用的最小文件描述符数量,默认1024minprocs: supervisord在成功启动前可用的最小进程描述符数量,默认200nocleanup: 防止supervisord在启动的时候清除已经存在的子进程日志文件childlogdir: 自动启动的子进程的日志目录user: supervisord的运行用户directory: supervisord以守护进程运行的时候切换到这个目录strip_ansi: 消除子进程日志文件中的转义序列
program 配置块
command: 启动程序使用的命令,可以是绝对路径或者相对路径process_name: 一个python字符串表达式,用来表示supervisor进程启动的这个的名称,默认值是%(program_name)snumprocs: Supervisor启动这个程序的多个实例,如果numprocs>1,则process_name的表达式必须包含%(process_num)s,默认是1numprocs_start: 一个int偏移值,当启动实例的时候用来计算numprocs的值priority: 权重,可以控制程序启动和关闭时的顺序,权重越低 : 越早启动,越晚关闭。默认值是999autostart: 如果设置为true,当supervisord启动的时候,进程会自动重启。autorestart: 值可以是false、true、unexpected。false : 进程不会自动重启,unexpected : 当程序退出时的退出码不是exitcodes中定义的时,进程会重启,true : 进程会无条件重启当退出的时候。startsecs: 程序启动后等待多长时间后才认为程序启动成功startretries: supervisord尝试启动一个程序时尝试的次数。默认是3exitcodes: 一个预期的退出返回码,默认是0,2。stopsignal: 当收到stop请求的时候,发送信号给程序,默认是TERM信号,也可以是 HUP, INT, QUIT, KILL, USR1, or USR2。stopwaitsecs: 在操作系统给supervisord发送SIGCHILD信号时等待的时间stopasgroup: 如果设置为true,则会使supervisor发送停止信号到整个进程组killasgroup: 如果设置为true,则在给程序发送SIGKILL信号的时候,会发送到整个进程组,它的子进程也会受到影响。user: 如果supervisord以root运行,则会使用这个设置用户启动子程序redirect_stderr: 如果设置为true,进程则会把标准错误输出到supervisord后台的标准输出文件描述符。stdout_logfile: 把进程的标准输出写入文件中,如果stdout_logfile没有设置或者设置为AUTO,则supervisor会自动选择一个文件位置。stdout_logfile_maxbytes: 标准输出log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小stdout_logfile_backups: 标准输出日志轮转备份的数量,默认是10,如果设置为0,则不备份stdout_capture_maxbytes: 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GBstdout_events_enabled: 如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发stderr_logfile: 把进程的错误日志输出一个文件中,除非redirect_stderr参数被设置为truestderr_logfile_maxbytes: 错误log文件达到多少后自动进行轮转,单位是KB、MB、GB。如果设置为0则表示不限制日志文件大小stderr_logfile_backups: 错误日志轮转备份的数量,默认是10,如果设置为0,则不备份stderr_capture_maxbytes: 当进程处于stderr capture mode模式的时候,写入FIFO队列的最大bytes值,单位可以是KB、MB、GBstderr_events_enabled: 如果设置为true,当进程在写它的stderr到文件描述符的时候,PROCESS_LOG_STDERR事件会被触发environment: 一个k/v对的list列表directory: supervisord在生成子进程的时候会切换到该目录umask: 设置进程的umask

