大家好,Supervisor 是一个 C/S 架构的进程监控与管理工具,本文将主要介绍其基本用法和部分高级特性,用于解决部署持久化进程的稳定性问题。
1.问题场景
在实际的工作中,往往会有部署持久化进程的需求,比如接口服务进程,又或者是消费者进程等。这类进程通常是作为后台进程持久化运行的。
一般的部署方法是通过nohup cmd &命令来部署。但是这种方式有个弊端是在某些情况下无法保证目标进程的稳定性运行,有的时候 nohup 运行的后台任务会因为未知原因中断,从而导致服务或者消费中断,进而影响项目的正常运行。
为了解决上述问题,通过引入Supervisor来部署持久化进程,提高系统运行的稳定性。
2.Supervisor 简介
Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems.
Supervisor 是一个 C/S 架构的进程监控与管理工具,其最主要的特性是可以监控目标进程的运行状态,并在其异常中断时自动重启。同时支持对多个进程进行分组管理。
完整特性详见官方文档 github 与 document。
3.部署流程
3.1 安装 Supervisor
通过pip命令安装 Supervisor 工具:
pip install supervisor
PS : 根据官方文档的说明 Supervisor 不支持在 windows 环境下运行
3.2 自定义服务配置文件
在安装完成后,通过以下命令生成配置文件到指定路径:
echo_supervisord_conf > /etc/supervisord.conf
配置文件的一些主要配置参数如下
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; 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=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;[group:thegroupname]