一、安装与简介
实验环境:
supervisor安装
sudo apt-get install supervisor
supervisor简介
python开发的通用进程管理工具,可以将命令行进程转换为后台的deamon进程来运行,通过fork/exec方式(后续有简要介绍)所管理的进程都做为supervisor的子进程来处理。
之前也用过类似supervisor的这种进程管理工具Monit,他的实现方式并不是作为子进程来执行,各有优劣,作为子进程执行可以更准确及时的获取子进程的状态信息,一旦子进程意外终止,可以快速自动的重启服务,但是当父进程supervisor意外终止时,所有子进程都会被操作系统接管,然后被一一杀死,也有一定的风险,不过若supervisor比较程序健壮,风险也会比较小一些。
supervisor提供了不少的方式可以用来管理进程,但是优先级方式来控制进程的启动顺序并非一个十分严格先后顺序,因此我们可能不得不在这个轮子的基础上制造新的轮子来满足我们的需求,不过这都是后话了,这几天就先来详细的分析一个这个supervisor的运行方式,然后分析源码,看他的运行原理。
补充内容:fork进程简述
Linux的进程分为代码段、数据段、堆栈段三个部分,一般CPU上都有上述三种段寄存器,以方便程序的运行。
代码段:存放了程序运行的代码,多个进程运行同一个程序,可以使用相同的代码段。
堆栈段:存放子程序的返回地址、子程序的参数以及程序的局部变量。
数据段:存放程序的全局变量、常数以及动态数据分配的内存空间(如malloc分配的内存空间)
fork
fork可以启动一个新进程,但是不是实际上的拷贝,而是逻辑上的复制。
CPU分配内存一般是以页为单位的,每一页都是实际物理内存的一个映像,像Intel的某些CPU,其一页内存大小约为4096字节。
程序的代码段、堆栈段、数据段都由很多页组成,在不发生改变时,进程实际上依然共用着统一段内存空间,只有中发生改变的时候,程序才从物理上将有区别的页分开,这样可以减少进程的内存开销。
典型用法:
- 创建新的进程来分别执行处理流程相似的工作,网络服务器的典型用法。
- 创建一个fork进程,然后通过exec来执行其他的程序。
在执行exec的时候,当前进程便死亡了,代码段、堆栈段、数据段皆改为新进程的,只留下了进程号。
二、supervisor配置简析
supervisor配置文件的位置
/etc/supervisor/supervisord.conf
要管理的子进程的配置文件所在文件夹的位置
/etc/supervisor/conf.d/*.conf
当然,也可以自己在之前的supervisor中的 [include] 中,加入
[include]
files = <path you difine>
# likes
files = /etc/supervisor/conf.d/*.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)
[inet_http_server] ;这一部分是最新添加的,其余部分基本保持来默认的设置
port = 0.0.0.0:9001
username = user
password = 123
[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
开始自带的配置文件,并没有很全面,但是基本可以运行,更多配置文件的介绍慢慢来。
[unix_http_server]
socked文件相关内容
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0700 ;这个是socket文件的权限
chown=uid:gid ;socket文件的owner
[inet_http_server]
http服务器,提供Web界面管理
[inet_http_server]
port = 0.0.0.0:9001
username = user
password = 123
[supervisord]
supervisord日志等相关配置信息
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=5000MB ;默认是50MB
logfile_backups=1000 ;默认是10
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ;默认是false
minfds=1024 ;默认是1024
minprocs=200 ;默认是200
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
[supervisorclt] && [rpcinterface:supervisor]
; 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
[include]
导入配置文件
[include]
files = /etc/supervisor/conf.d/*.conf