跳转至

supervisord 命令


2016-07-12 by dongnan

环境

CentOS 6.7 amd64
supervisor-2.1-9

什么是 supervisor?

supervisor 是用 Python 开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台 daemon,并监控进程状态,异常退出时能自动重启。

一个服务程序要可靠地在后台运行,我们需要把它做成 daemon,最好还能监控进程状态,在意外结束时能自动重启。

安装

yum install supervisor

配置

配置文件类似如下

# 执行命令
awk '! /^(;|$)/' /etc/supervisord.conf

[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
#定义NotifyDaemon
[program:NotifyDaemon]
command=/usr/local/php/bin/php /var/www/html/CronRoot/NotifyDaemon.php

启动

/etc/init.d/supervisord start

# 或者使用命令启动
/usr/bin/supervisord -c /etc/supervisord.conf

验证

终端1,监控 supervisor 日志,终端2 kill 32254 进程。

# 执行命令
tail /var/log/supervisor/supervisord.log
2016-07-29 13:29:39,230 CRIT Supervisor running as root (no user in config file)
2016-07-29 13:29:39,251 INFO supervisord started with pid 32253
2016-07-29 13:29:39,258 INFO spawned: 'NotifyDaemon' with pid 32254
2016-07-29 13:29:40,261 INFO success: NotifyDaemon entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2016-07-29 13:30:52,341 INFO exited: NotifyDaemon (terminated by SIGTERM; not expected)
2016-07-29 13:30:52,342 INFO received SIGCLD indicating a child quit
2016-07-29 13:30:53,349 INFO spawned: 'NotifyDaemon' with pid 32463
2016-07-29 13:30:54,352 INFO success: NotifyDaemon entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

帮助

/usr/bin/supervisord --help
supervisord -- run a set of applications as daemons.

Usage: /usr/bin/supervisord [options]

Options:
-c/--configuration URL -- configuration file or URL
-n/--nodaemon -- run in the foreground (same as 'nodaemon true' in config file)
-h/--help -- print this usage message 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
                   orphaned child log files, etc.) at startup.
-w/--http_port SOCKET -- the host/port that the HTTP server should listen on
-g/--http_username STR -- the username for HTTP auth
-r/--http_password STR -- the password for HTTP auth
-a/--minfds NUM -- the minimum number of file descriptors for start success
--minprocs NUM  -- the minimum number of processes available for start success

小结

  • 通过 kill NotifyDaemon 进程 ,supervisor 可以自运行新的 NotifyDaemon 进程,防止后台服务异常终止。
  • 通过 supervisor 日志,也可以观察整个 supervisord 运行过程。
回到页面顶部