环境要求
- CentOS7
- Python2.7.5
- Supervisor 3.1.4
supervisor版本依赖于python版本选择与python对应版本的supervisor。supervisor暂不支持windows操作系统。
supervisor安装说明
如果系统对python的版本无特殊要求,则可直接使用CentOS7自带的python版本。CentOS7默认python的版本为2.7。为了安装方便,可直接使用CentOS7自带的安装工具yum
进行安装。yum
安装默认安装与系统环境相匹配可用的supervisor
版本。一共有三种安装方式,如下所示:
-
yum install -y supervisor
安装。 -
easy_install supervisor
安装(python安装工具),默认安装最新版本 -
pip supervisor
安装(python安装工具),默认安装最新版本
以上三种方式安装主要根据自己的需求以及系统环境选择合适安装方式。
下面主要讲yum
安装步骤
如果有配置
yum
包国内资源地址以及扩展仓库,可忽略此步骤:yum -y install epel-release
,替换国内镜像源,此处不做详细介绍。使用
yum install -y supervisor
进行安装服务。-
安装完成后,检查是否存在配置文件。如果没有配置文件则使用
echo_supervisord_conf > /etc/supervisord.conf
创建配置文件。默认路径为。yum
会自动生成supervisord.conf
(主程序配置以及相关说明)和supervisord.d
文件夹(主要相关独立配置存放路径)。$ whereis supervisord # 可检查配置文件等相关路径,控制台输出 supervisord: /usr/bin/supervisord /etc/supervisord.conf /etc/supervisord.d
supervisor配置说明
; Sample supervisor config file.
[unix_http_server]
file=/var/run/supervisor/supervisor.sock ; socket文件的路径,supervisorctl用XML_RPC和supervisord通信就是
; 通过它进行的。如果不设置的话,supervisorctl也就不能用了。
;chmod=0700 ; 修改上面的那个socket文件的权限为0700。
; 不设置的话,默认为0700。 非必须设置
;chown=nobody:nogroup ; 这个一样,修改上面的那个socket文件的属组为user.group
; 不设置的话,默认为启动supervisord进程的用户及属组。非必须设置
;username=user ; 使用supervisorctl连接的时候,认证的用户
; 不设置的话,默认为不需要用户。 非必须设置
;password=123 ; 和上面的用户名对应的密码,可以直接使用明码,也可以使用SHA加密
; 如:{SHA}82ab876d1387bfafe46cc1c8a2ef074eae50cb1d
; 默认不设置。。。非必须设置
;[inet_http_server] ; 侦听在TCP上的socket,Web Server和远程的supervisorctl都要用到他。
; 不设置的话,默认为不开启。非必须设置
;port=127.0.0.1:9001 ; 这个是侦听的IP和端口,侦听所有IP用 :9001或*:9001。
; 这个必须设置,只要上面的[inet_http_server]开启了,就必须设置它
;username=user ; 这个和上面的uinx_http_server一个样。非必须设置
;password=123 ; 这个也一个样。非必须设置
[supervisord] ; 这个主要是定义supervisord这个服务端进程的一些参数的,
; 这个必须设置,不设置,supervisor就不用干活了
logfile=/web/logs/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log)
; 这个是supervisord这个主进程的日志路径,注意和子进程的日志不搭嘎。
; 默认路径$CWD/supervisord.log,$CWD是当前目录。。非必须设置
logfile_maxbytes=50MB ; 这个是上面那个日志文件的最大的大小,当超过50M的时候,会生成一个新的日志文件。当设置为0时,表示不限制文件大小
; 默认值是50M,非必须设置。
logfile_backups=10 ; 日志文件保持的数量,上面的日志文件大于50M时,就会生成一个新文件。文件
; 数量大于10时,最初的老文件被新文件覆盖,文件数量将保持为10
; 当设置为0时,表示不限制文件的数量。
; 默认情况下为10。。。非必须设置
loglevel=info ; 日志级别,有critical, error, warn, info, debug, trace, or blather等
; 默认为info,非必须设置项
pidfile=/var/run/supervisord.pid ; supervisord的pid文件路径。
; 默认为$CWD/supervisord.pid。。。非必须设置
nodaemon=false ; 如果是true,supervisord进程将在前台运行
; 默认为false,也就是后台以守护进程运行。。。非必须设置
minfds=1024 ; 这个是最少系统空闲的文件描述符,低于这个值supervisor将不会启动。
; 系统的文件描述符在这里设置cat /proc/sys/fs/file-max
; 默认情况下为1024。。。非必须设置
minprocs=200 ; 最小可用的进程描述符,低于这个值supervisor也将不会正常启动。
; ulimit -u这个命令,可以查看linux下面用户的最大进程数
; 默认为200。。。非必须设置
;umask=022 ; 进程创建文件的掩码
; 默认为022。。非必须设置项
;user=chrism ; 这个参数可以设置一个非root用户,当我们以root用户启动supervisord之后。
; 我这里面设置的这个用户,也可以对supervisord进行管理
; 默认情况是不设置。。。非必须设置项
;identifier=supervisor ; (supervisord identifier, default is 'supervisor')
;directory=/tmp ; (default is not to cd during start)
;nocleanup=true ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value ; (key value pairs to add to environment)
;strip_ansi=false ; (strip ansi escape codes in logs; def. false)
; 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/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 http_username if set
;password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.
;[program:theprogramname] ; 这个就是咱们要管理的子进程了,":"后面的是名字,最好别乱写和实际进程
有点关联最好。这样的program我们可以设置一个或多个,一个program就是
要被管理的一个进程
;command=/bin/cat ; 这个就是我们的要启动进程的命令路径了(命令更准确点),可以带参数
例子:/home/test.py -a 'hehe'
有一点需要注意的是,我们的command只能是那种在终端运行的进程,不能是
守护进程。这个想想也知道了,比如说command=service httpd start。
httpd这个进程被linux的service管理了,我们的supervisor再去启动这个命令
这已经不是严格意义的子进程了。
这个是个必须设置的项
;process_name=%(program_name)s ; 这个是进程名,如果我们下面的numprocs参数为1的话,就不用管这个参数
了,它默认值%(program_name)s也就是上面的那个program冒号后面的名字,
但是如果numprocs为多个的话,那就不能这么干了。想想也知道,不可能每个
进程都用同一个进程名吧。
;numprocs=1 ; 启动进程的数目。当不为1时,就是进程池的概念,注意process_name的设置
默认为1 。。非必须设置
;directory=/tmp ; 进程运行前,会前切换到这个目录
默认不设置。。。非必须设置
;umask=022 ; 进程掩码,默认none,非必须
;priority=999 ; 子进程启动关闭优先级,优先级低的,最先启动,关闭的时候最后关闭
默认值为999 。。非必须设置
;autostart=true ; 如果是true的话,子进程将在supervisord启动后被自动启动
默认就是true 。。非必须设置
;autorestart=true ; 这个是设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected
和true。如果为false的时候,无论什么情况下,都不会被重新启动,
如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的退
出码的时候,才会被自动重启。当为true的时候,只要子进程挂掉,将会被无
条件的重启
;startsecs=10 ; 这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启
动成功了
默认值为1 。。非必须设置
;startretries=3 ; 当进程启动失败后,最大尝试启动的次数。。当超过3次后,supervisor将把
此进程的状态置为FAIL
;exitcodes=0,2 ; 注意和上面的的autorestart=unexpected对应。。exitcodes里面的定义的
退出码是expected的。
;stopsignal=QUIT ; 进程停止信号,可以为TERM, HUP, INT, QUIT, KILL, USR1, or USR2等信号
默认为TERM 。。当用设定的信号去干掉进程,退出码会被认为是expected
非必须设置
;stopwaitsecs=10 ; 这个是当我们向子进程发送stopsignal信号后,到系统返回信息
给supervisord,所等待的最大时间。 超过这个时间,supervisord会向该
子进程发送一个强制kill的信号。
默认为10秒。。非必须设置
;user=chrism ; 如果supervisord是root启动,我们在这里设置这个非root用户,可以用来
管理该program,默认不设置。。。非必须设置项
;redirect_stderr=true ; 如果为true,则stderr的日志会被写入stdout日志文件中
默认为false,非必须设置
;stdout_logfile=/a/path ; 子进程的stdout的日志路径,可以指定路径,AUTO,none等三个选项。
设置为none的话,将没有日志产生。设置为AUTO的话,将随机找一个地方
生成日志文件,而且当supervisord重新启动的时候,以前的日志文件会被
清空。当 redirect_stderr=true的时候,sterr也会写进这个日志文件
;stdout_logfile_maxbytes=1MB ; 日志文件最大大小,和[supervisord]中定义的一样。默认为50
;stdout_logfile_backups=10 ; 和[supervisord]定义的一样。默认10
;stdout_capture_maxbytes=1MB ; 这个东西是设定capture管道的大小,当值不为0的时候,子进程可以从stdout
发送信息,而supervisor可以根据信息,发送相应的event。
默认为0,为0的时候表达关闭管道。。。非必须项
;stdout_events_enabled=false ; 当设置为ture的时候,当子进程由stdout向文件描述符中写日志的时候,将
触发supervisord发送PROCESS_LOG_STDOUT类型的event
默认为false。。。非必须设置
;stderr_logfile=/a/path ; 这个东西是设置stderr写的日志路径,当redirect_stderr=true。这个就不用
设置了,设置了也是白搭。因为它会被写入stdout_logfile的同一个文件中
默认为AUTO,也就是随便找个地存,supervisord重启被清空。。非必须设置
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; 这个是该子进程的环境变量,和别的子进程是不共享的
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.
;[eventlistener:theeventlistenername]; 和program的地位是一样的,也是suopervisor启动的子进
程,不过它干的活是订阅supervisord发送的event。他的名字就叫
listener了。我们可以在listener里面做一系列处理,比如报警等等
;command=/bin/eventlistener ; 这个和上面的program一样,表示listener的可执行文件的路径(命令更准确点)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event事件的类型,也就是说,只有写在这个地方的事件类型。才会被发送
;buffer_size=10 ; 这个是event队列缓存大小,单位不太清楚,楼主猜测应该是个吧。当buffer
超过10的时候,最旧的event将会被清除,并把新的event放进去。
默认值为10。。非必须选项
;directory=/tmp ; 进程执行前,会切换到这个目录下执行
默认为不切换。。。非必须
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;autorestart=unexpected ; restart at unexpected quit (default: unexpected)
;startsecs=10 ; number of secs prog must stay running (def. 1)
;startretries=3 ; max # of serial start failures (default 3)
;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;environment=A=1,B=2 ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 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] ; 加载独立配置,我们可以把配置信息写到多个文件中,然后include过来
files = supervisord.d/*.ini ; 独立配置路径以及加载哪些配置。有的是conf,有的是ini格式的。yum安装为ini格式。
以下为运行tp5队列守护进程的程序代码,所在服务器为192.168.0.30中相关进程。配置路径:/etc/supervisord.d
-
用户门卡队列进程配置,如下:
[program:redis_queue_card_task] ;项目名称 ; 程序的启动目录 directory = /web/www/redis_queue ; 启动命令 command = php think queue:work --queue cardTask --daemon process_name=%(program_name)s_%(process_num)02d ; 开启的进程数量 numprocs = 3 ; 在 supervisord 启动的时候也自动启动 autostart = true ; 启动 5 秒后没有异常退出,就当作已经正常启动了 startsecs = 5 ; 程序异常退出后自动重启 autorestart = true ; 启动失败自动重试次数,默认是 3 startretries = 10 ; 用哪个用户启动 user = web ; 把 stderr 重定向到 stdout,默认 false redirect_stderr = true ; stdout 日志文件大小,默认 50MB stdout_logfile_maxbytes = 50MB ; stdout 日志文件备份数 stdout_logfile_backups = 20 ; stdout 日志文件,需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /web/logs/supervisord/cardTask.log loglevel=info ; max num secs to wait b4 SIGKILL (default 10) stopwaitsecs=180
-
用户队列进程配置,如下:
[program:redis_queue_user_task] ;项目名称 ; 程序的启动目录 directory = /web/www/redis_queue ; 启动命令 command = php think queue:work --queue userTask --daemon process_name=%(program_name)s_%(process_num)02d ; 开启的进程数量 numprocs = 3 ; 在 supervisord 启动的时候也自动启动 autostart = true ; 启动 5 秒后没有异常退出,就当作已经正常启动了 startsecs = 5 ; 程序异常退出后自动重启 autorestart = true ; 启动失败自动重试次数,默认是 3 startretries = 10 ; 用哪个用户启动 user = web ; 把 stderr 重定向到 stdout,默认 false redirect_stderr = true ; stdout 日志文件大小,默认 50MB stdout_logfile_maxbytes = 50MB ; stdout 日志文件备份数 stdout_logfile_backups = 20 ; stdout 日志文件,需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /web/logs/supervisord/userTask.log loglevel=info ; max num secs to wait b4 SIGKILL (default 10) stopwaitsecs=180
配置守护进程程序说明
- 启动|停止|重启服务:
service supervisord start|stop|restart
; - 重新加载子进程配置:
supervisorctl reread
; - 更新子进程:
supervisorctl update
; - 开机启动:
systemctl enable supervisord
;
提供两种命令查看启动效果如下:
$ service supervisord status
Redirecting to /bin/systemctl status supervisord.service
● supervisord.service - Process Monitoring and Control Daemon
Loaded: loaded (/usr/lib/systemd/system/supervisord.service; enabled; vendor preset: disabled)
Active: active (running) since 四 2019-06-27 15:11:37 CST; 19h ago
Main PID: 14460 (supervisord)
CGroup: /system.slice/supervisord.service
├─14460 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
├─17689 php think queue:work --queue cardTask --daemon
├─17690 php think queue:work --queue cardTask --daemon
├─17691 php think queue:work --queue cardTask --daemon
├─17692 php think queue:work --queue userTask --daemon
├─17693 php think queue:work --queue userTask --daemon
└─17694 php think queue:work --queue userTask --daemon
6月 27 15:11:36 test.yichuang.com systemd[1]: Starting Process Monitoring and Control Daemon...
6月 27 15:11:37 test.yichuang.com systemd[1]: Started Process Monitoring and Control Daemon.
$ supervisorctl status
redis_queue_card_task:redis_queue_card_task_00 RUNNING pid 17690, uptime 17:53:52
redis_queue_card_task:redis_queue_card_task_01 RUNNING pid 17689, uptime 17:53:52
redis_queue_card_task:redis_queue_card_task_02 RUNNING pid 17691, uptime 17:53:52
redis_queue_user_task:redis_queue_user_task_00 RUNNING pid 17693, uptime 17:53:52
redis_queue_user_task:redis_queue_user_task_01 RUNNING pid 17694, uptime 17:53:52
redis_queue_user_task:redis_queue_user_task_02 RUNNING pid 17692, uptime 17:53:52
错误使用姿势以及注意事项
报错exit status 1; not expected
我大致总结了以下几点:
- 配置文件错误:
- 独立配置文件格式如果是ini格式,注释另起一行。如果是conf,则按照conf格式要求编写配置。否则可能会报错;
- 运行脚本无可执行权限。
- 日志文件不存在,如果是自定义的日志,需要自动创建,并且修改为合适的权限如:0755,0766。
报错exit status 0; not expected
这个说明至少脚本运行了。只是因为脚本可能是非常驻进程脚本,导致无法常驻进程运行,如:自己的测试脚本。
echo date("Y-m-d H:i:s",time()) ."【info】This is test message!";
如果监听supervisord.log(查看配置文件可知道具体路径),则可以看出:
2019-06-27 15:22:56,588 INFO exited: test_file_00 (exit status 0; not expected)
2019-06-27 15:22:56,588 INFO exited: test_file_01 (exit status 0; not expected)
2019-06-27 15:22:56,588 INFO exited: test_file_02 (exit status 0; not expected)
2019-06-27 15:22:58,591 INFO spawned: 'test_file_00' with pid 14694
2019-06-27 15:22:58,592 INFO spawned: 'test_file_01' with pid 14695
2019-06-27 15:22:58,593 INFO spawned: 'test_file_02' with pid 14696
2019-06-27 15:22:58,647 INFO exited: test_file_00 (exit status 0; not expected)
2019-06-27 15:22:58,647 INFO exited: test_file_01 (exit status 0; not expected)
2019-06-27 15:22:58,647 INFO exited: test_file_02 (exit status 0; not expected)
2019-06-27 15:23:01,652 INFO spawned: 'test_file_00' with pid 14697
2019-06-27 15:23:01,653 INFO spawned: 'test_file_01' with pid 14698
2019-06-27 15:23:01,653 INFO spawned: 'test_file_02' with pid 14699
2019-06-27 15:23:01,709 INFO exited: test_file_00 (exit status 0; not expected)
2019-06-27 15:23:01,709 INFO exited: test_file_01 (exit status 0; not expected)
2019-06-27 15:23:01,710 INFO exited: test_file_02 (exit status 0; not expected)
2019-06-27 15:23:02,711 INFO gave up: test_file_00 entered FATAL state, too many start retries too quickly
2019-06-27 15:23:02,711 INFO gave up: test_file_01 entered FATAL state, too many start retries too quickly
2019-06-27 15:23:02,711 INFO gave up: test_file_02 entered FATAL state, too many start retries too quickly
如果在[program:xxxxx]中配置了程序的输出日志,则可看到具体错误以及是否运行。
以上两点主要提供排查的思路,仅供参考。