Install, configure, and use a supervisor.
Supervisor: A Process Control System in the C/S architecture that allows you to monitor and manage processes in UNIX-like systems. It is often used to manage processes related to a user or project.
Components
Supervisord: service daemon
Supervisorctl: command line Client
Web Server: provides a WEB operation interface equivalent to the supervisorctl Function
XML-RPC Interface: XML-RPC Interface
Install
You can directly use the YUM source for installation on the centos platform.
Yum info supervisor
Sudo yum install supervisor
Sudo chkconfig supervisord on
Server start/stop
Sudo/etc/init. d/supervisord {start | stop | status | restart | reload | force-reload | condrestart}
Logs
/Var/log/supervisor/supervisord. log
Configuration File
Sudo vim/etc/supervisord. conf
It generally includes the following configurable parts:
[Unix_http_server]
[Inet_http_server]
[Supervisord]
[Supervisorctl]
[Program: x]
[Include]
[Group: x]
[Fcgi-program: x]
[Eventlistener: x]
[Rpcinterface: x]
Pay attention to the following two parts:
Configure the process to be monitored in [program: x]
[Group: x] process group to be monitored
Configuration example
[supervisord]http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server);http_port=127.0.0.1:9001 ; (alternately, ip_address:port specifies AF_INET);sockchmod=0700 ; AF_UNIX socketmode (AF_INET ignore, default 0700);sockchown=nobody.nogroup ; AF_UNIX socket uid.gid owner (AF_INET ignores);umask=022 ; (process file creation umask;default 022)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);nocleanup=true ; (don't clean up tempfiles at start;default false);http_username=user ; (default is no username (open system));http_password=123 ; (default is no password (open system));childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP);user=chrism ; (default is current user, required if root);directory=/tmp ; (default is not to cd during start);environment=KEY=value ; (key value pairs to add to environment)[supervisorctl]serverurl=unix:///var/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 http_username if set;password=123 ; should be same as http_password if set;prompt=mysupervisor ; cmd line prompt (default "supervisor"); 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:example];command=/bin/echo; the program (relative uses PATH, can take args);priority=999 ; the relative start priority (default 999);autostart=true ; start at supervisord start (default: true);autorestart=true ; retstart at unexpected quit (default: true);startsecs=10 ; number of secs prog must stay running (def. 10);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 before SIGKILL (default 10);user=chrism ; setuid to this UNIX account to run the program;log_stdout=true ; if true, log program stdout (default true);log_stderr=true ; if true, log program stderr (def false);logfile=/var/log/supervisor.log ; child log path, use NONE for none; default AUTO;logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB);logfile_backups=10 ; # of logfile backups (default 10)
";" Is a comment. Each parameter has a clear meaning. You can refer to the official manual to learn more about the experiment. Key words: parameters in [program: example]
; Command =/bin/echo; process to be started by the supervisor at startup. The relative or absolute path is acceptable. If it is a relative PATH, it will be searched from the $ PATH change of supervisord. The command can contain parameters .; Priority = 999 indicates the order in which the process starts and stops. Low priority indicates that the process is disabled after being started. A high priority indicates that a process is disabled first when it is started and later when it is started .; Autostart = true: whether to start with supervisord; autorestart = true: whether to automatically restart after the process unexpectedly exits; startsecs = 10 how long the process continues to run is considered to be started successfully; startretries = 3 the number of consecutive retries that failed to restart; exitcodes = If autostart is set to unexpected and the monitored process does not exit because supervisord stops, if the exit code of the process is not in the exitcode list, supervisord will restart the process. stopsignal = QUIT will kill the process signal. stopwaitsecs = 10 will send stopsignal to the process and wait for the OS to return SIGCHILD time to supervisord. If timeout occurs, supervisord will use SIGKILL to kill the process.
Configuration monitored by the supervisor by the producer and consumer processes in a Rabbitmq project: (other parts in the configuration are omitted)
[program:worker_for_summary]command=/home/op1/test_db_monitor/worker_for_summary.pylogfile=/var/log/worker_for_summary.py.supervisor.log[program:worker_for_detail_all]command=/home/op1/test_db_monitor/worker_for_detail_all.pylogfile=/var/log/worker_for_detail_all.py.supervisor.log[program:worker_for_detail_recent]command=/home/op1/test_db_monitor/worker_for_detail_recent.pylogfile=/var/log/worker_for_detail_recent.py.supervisor.log[program:publisher_for_summary]command=/home/op1/test_db_monitor/publisher_for_summary.pylogfile=/var/log/publisher_for_summary.py.supervisor.log[program:publisher_for_summary_nt]command=/home/op1/test_db_monitor/publisher_for_summary_nt.pylogfile=/var/log/publisher_for_summary_nt.py.supervisor.log[program:publisher_for_detail]command=/home/op1/test_db_monitor/publisher_for_detail.pylogfile=/var/log/publisher_for_detail.py.supervisor.log[program:publisher_for_detail_nt]command=/home/op1/test_db_monitor/publisher_for_detail_nt.pylogfile=/var/log/publisher_for_detail_nt.py.supervisor.log[group:dbmonitor_consumer]programs=worker_for_summary, worker_for_detail_all, worker_for_detail_recentpriority=1log_stderr=truelogfile_maxbytes=1MB[group:dbmonitor_publisher]programs=publisher_for_summary, publisher_for_summary_nt,publisher_for_detail,publisher_for_detail_ntpriority=999log_stderr=truelogfile_maxbytes=1MB
In the [program: x] section, the producer and consumer processes are configured respectively. Worker _ * is the consumer process, and publisher _ * is the producer process.
In the [group: x] section, the monitored process information is divided into two groups: producer and consumer, with different priorities.
After the configuration is complete, start supervisord
sudo /etc/init.d/supervisord start
You can see that the processes configured are running in the background.
After a process is stopped, the supervisor will immediately restart the process.
Stop a supervisor
sudo /etc/init.d/supervisord stop
You can see that all processes configured are stopped.
You can use supervisorctl to view the process of management and monitoring:
[Op1 @ SVR1631HP360 ~] $ Sudo parse RUNNING pid 27557, uptime 0: 00: Invalid RUNNING pid 27567, uptime 0: 00: 45publisher_for_summary RUNNING pid 27566, uptime 0: 00: 45publisher_for_summary_nt RUNNING pid 27568, uptime 0: 00: 45worker_for_detail_all RUNNING pid 27581, uptime 0: 00: 45worker_for_detail_recent RUNNING pid 27582, uptime 0: 00: 45worker_for_summary RUNNING pid 27559, uptime 0:00:45 # use help to learn more about commands. supervisor> helpshortented commands (type help <topic> ): ========================================================== EOF exit maintail quit restart start stopclear help open reload shutdown status tailsupervisor> help stopstop <processname> Stop a process. stop <processname> Stop multiple processesstop allStop all processes When all processes are stopped, they are stopped in reverse priority order (see config file) supervisor> help statusstatusGet all process status info. status <name> Get status on a single process by name. status <name> Get status on multiple named processes. # stop a process supervisor> stop supervisor: stopped # view the current State supervisor> statusublisher_for_detail RUNNING pid 27557, uptime 0: 05: 41publisher_for_detail_nt RUNNING pid 27567, uptime 0: 05: 41publisher_for_summary STOPPED Feb 27 RUNNING pid 27568, uptime 0: 05: Invalid RUNNING pid 27581, uptime 0: 05: Invalid RUNNING pid 27582, uptime 0: 05: 41worker_for_summary RUNNING pid 27559, uptime 0:05:41 # The process stopped by supervisorctl will not be automatically restarted # enable the stopped process supervisor> start supervisor: startedsupervisor> statusublisher_for_detail RUNNING pid 27557, uptime 0: 08: invalid RUNNING pid 27567, uptime 0: 08: 02publisher_for_summary RUNNING pid 3035, uptime 0: 00: 04publisher_for_summary_nt RUNNING pid 27568, uptime 0: 08: Invalid RUNNING pid 27581, uptime 0: 08: RUNNING pid 27582, uptime 0: 08: 02worker_for_summary RUNNING pid 27559, uptime 0:08:02 # stop all processes supervisor> stop RUNNING: stopped: stoppedpublisher_for_summary: stopped: stoppedpublisher_for_detail: starting> stopping STOPPED Feb 27 Starting STOPPED Feb 27 Starting STOPPED Feb 27 stopping STOPPED Feb 27 stopping STOPPED Feb 27 Starting STOPPED Feb 27 stopping STOPPED Feb 27 Starting STOPPED Feb 27 PM # enabling all processes supervisor> start allpublisher_for_detail: usage: startedpublisher_for_summary: Usage: startedsupervisor> invalid RUNNING pid 5111, uptime 0: 00: Invalid RUNNING pid 5141, uptime 0: 00: 15publisher_for_summary RUNNING pid 5135, uptime 0: 00: 15publisher_for_summary_nt RUNNING pid 5147, uptime 0: 00: Invalid RUNNING pid 5153, uptime 0: 00: Invalid RUNNING pid 5159, uptime 0: 00: 14worker_for_summary RUNNING pid 5112, uptime 0:00:15
For more information, see the official manual.
Http://supervisord.org/