Basic usage of Process Monitoring tool supervisor in Linux system

Source: Internet
Author: User
Tags chmod readline socket unix domain socket in python

Supervisor is a C/s system that allows users to control some processes on Unix-like systems. It has the following characteristics:

1 simple

Supervisor through the INI format configuration file is easy to master, it provides a lot of configuration options for each process, so you can easily restart the process or automatic rotation log.

2 Unified

Supervisor provides a unified way to start, stop, monitor your processes, processes can be individually controlled, and can be controlled in groups. You can configure supervisor at the local or remote command line or Web interface.

3 Effective

Supervisor starts its subprocess through fork/exec, and the child process is not a daemon. When a process terminates, the operating system sends a signal to supervisor immediately, rather than relying on the PID file as other solutions.

4 Scalable

Supervisor contains a simple event notification protocol, so any program can monitor it and provide an XML-RPC control interface.

5 compatible

Other platforms are available in addition to the Windows platform.

Components of the Supervisor system:

Supervisord:

The service starts the Supervisord service, which calls itself to start the subroutine, responds to commands from the client, restarts the crash or exits the process, logs the output of the process, and collects event information. The configuration file for this service is/etc/supervisor/supervisord.conf

Supervisorctl:

The client's command-line tool, which provides a class shell interface through which you can connect to different Supervisord processes to manage their own subroutines. A client command communicates with a service via a UNIX socket or TCP, and the server can require the client to provide authentication before being able to operate ([Supervisorctl]).

Web Server:

A small web interface is integrated into the SUPERVISORCTL and can be accessed ([Inet_http_server]) after restarting the Supervisord.

XML-RPC Interface:

Just as HTTP provides a Web UI, it also provides an XML-RPC interface to control supervisor and programs that run by it.

Installation:

Supervisor is written in Python, obviously with Easy_install, Pip can be installed, I lazy, direct apt-get, ubuntu14.04 under the installation after the version is 3.0b2.

Launch of the Supervisor service

In fact, start supervisor is very simple, supervisord-h see, the simplest C-root configuration file can:

The code is as follows:
Supervisord-run a set of applications as daemons.
Usage:/usr/bin/supervisord [Options]

Options:
-c/--configuration FILENAME--configuration file
-n/--nodaemon--run in the foreground (same as ' Nodaemon true ' config file)
-h/--help--Print this usage and exit
-v/--version--Print Supervisord version number 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
Old automatic child log files) at startup.
-a/--minfds NUM--The minimum number of file descriptors for start success
-t/--strip_ansi--strip ANSI escape codes from process output
--minprocs NUM--The minimum number of processes available for start success
--profile_options Options--run Supervisord under Profiler and output
Results based on OPTIONS, which is a comma-sep ' d
List of ' cumulative ', ' calls ', and/or ' callers ',
e.g. ' cumulative,callers ')
But since I am lazy people are installed with Apt-get, the installation package specifications must conform to the style of the Debian system, direct service supervisor start can start, wait, we have not configured Supervisor configuration file, the launch has no effect. We'll explain the configuration file configuration later.

Use of SUPERVISORCTL clients

There are two modes of supervisorctl, one is interaction mode, the other is command-line mode. Enter Supervisorctl direct return at the command line to enter interactive mode.

The code is as follows:
SUPERVISORCTL-control applications run by Supervisord to the CMD line.

Usage:/usr/bin/supervisorctl [Options] [action [arguments]]
Options:
-c/--configuration--Configuration file path (default/etc/supervisor.conf)
-h/--help--Print usage message and exit
-i/--interactive--Start an interactive shell after executing commands
-s/--serverurl URL--URL on which Supervisord server is listening
(default "http://localhost:9001").
-u/--username--username to use for authentication with server
-p/--password--Password to use for authentication with server
-r/--history-file--Keep a ReadLine history (if ReadLine is available)

The code is as follows:
Action [arguments]--below

The Actions are commands like "tail" or "stop". If-i is specified or no action
Specified on the command line, a "shell" interpreting actions typed
Interactively is started. Use the action ' help ' to find out about available
Actions.
Supervisor Power-on Self-starter

If you are a PIP or a Easy_install installation, the boot service is a real hassle. However, the authorities have given some examples of rc.d scripts, on GitHub, but since I was apt-get installed, it is clear that this boot from the boot is not to worry about, with Debian update-rc.d can be done.

Supervisor Process Security

Since the use of supervisor to ensure the normal operation of other processes, but in case the supervisor process hangs up, we can use Daemontools to ensure that the supervisor normal operation, similar to the monitoring of monitoring.

The Supervisord configuration file consists primarily of several configuration segments, and the configuration items are rendered in k/v format, and the following is a look at how the configuration ends are configured:

[Unix_http_server]

The parameter item in the configuration block represents an HTTP server that is listening on the socket, and if the [Unix_http_server] block is not in the configuration file, the HTTP server based on the socket is not started.

File: Files path to a UNIX domain socket, HTTP/XML-RPC will be listening on this
chmod: Modifying the mode of UNIX domain socket at startup
Chown: Modifying the owner of the socket file
Username:http server's user name at the time of authentication
Password: authentication password <span style= "font-family: ' Microsoft Yahei '; font-size:16px;line-height:1.5;" ></span>
eg

The code is as follows:
[Unix_http_server]
File =/tmp/supervisor.sock
chmod = 0777
chown= Nobody:nogroup
Username = user
Password = 123
[Inet_http_server]

The parameter entry in the configuration block represents an HTTP server that listens on TCP, and if the [Inet_http_server] block is not in the configuration file, TCP-based HTTP server is not started.

PORT:TCP Listener Address and Port (Ip:port), this address will be monitored by http/xml-rpc
Username:http server's user name at the time of authentication
Password: authentication password
eg

The code is as follows:
[Inet_http_server]
Port = 127.0.0.1:9001
Username = user
Password = 123
[Supervisord]

The parameter entries for the

Configuration block are global configuration items about the Supervisord process. The
Logfile:log file path
Logfile_maxbytes:log The number of files to rotate automatically, in kilobytes, MB, GB. Setting to 0 does not limit the log file size
Logfile_backups: The number of rotation log backups, the default is 10, and if set to 0, do not back up
Loglevel:error, warn, info, debug, Trace, blather, critical
Pidfile:pid file path
Umask:umask value, default 022
Nodaemon: If set to True, Supervisord starts in foreground instead of daemon
Minfds:supervisord The minimum number of file descriptors available before successful startup, default 1024
Minprocs:supervisord The minimum number of process descriptors available before a successful startup, default
Nocleanup: Prevents Supervisord from clearing an existing subprocess log file at startup
Childlogdir: Log directory for automatically started subprocess
User:supervisord run user
Directory:supervisord to this directory when the daemon is running
Strip_ansi: Eliminating escape sequences in child process log files
Environment: A list of k/v pairs
Eg:

The code is as follows:
[Supervisord]
LogFile =/tmp/supervisord.log
Logfile_maxbytes = 50MB
logfile_backups=10
LogLevel = info
Pidfile =/tmp/supervisord.pid
Nodaemon = False
Minfds = 1024
Minprocs = 200
Umask = 022
user = Chrism
Identifier = Supervisor
Directory =/tmp
Nocleanup = True
Childlogdir =/tmp
Strip_ansi = False
Environment = key1= "value1", key2= "value2"
[Supervisorctl]

This configuration block parameter is about SUPERVISORCTL

ServerURL: This URL is used to access the Supervisord service (http://localhost:9001), or a sockets file (Unix:///absolute/path/to/file.sock)
Username:supervisorctl Authenticated Users connected to Supervisord
Password: authentication password
Prompt: Default is supervisor
History_file:history file path
eg

The code is as follows:
[Supervisorctl]
ServerURL = Unix:///tmp/supervisor.sock
Username = Chris
Password = 123
prompt = Mysupervisor
[Program:x]

The configuration block contains one or more program sections that show Supervisord which programs to control. The header of the configuration block has a fixed format, a keyword program, followed by a colon, followed by the name of the application. For example: [Program:foo],foo is the name of the program, in the use of SUPERVISORCTL to operate the program, is indicated by Foo.

Command: Commands used by the startup program, either absolute or relative
Process_name: A python string expression that represents the name of the supervisor process startup, and the default is% (program_name) s
Numprocs:supervisor to start multiple instances of this program, if numprocs>1, the process_name expression must contain% (Process_num) s, default is 1
Numprocs_start: An int offset value that is used to compute the value of Numprocs when the instance is started
Priority: Weight, you can control the start and shutdown sequence, the lower the weight: The sooner you start, the later the shutdown. The default value is 999
Autostart: If set to true, the process will automatically restart when the Supervisord is started.
AutoRestart: Value can be false, true, unexpected. False: The process does not automatically restart, unexpected: The process restarts when the exit code is not defined in Exitcodes when the program exits, true: The process will restart unconditionally when exiting.
Startsecs: How long to wait after the program starts to think that the program started successfully
Startretries:supervisord the number of attempts to start a program. The default is 3
Exitcodes: An expected exit return code, the default is 0, 2.
Stopsignal: When receiving a stop request, send a signal to the program, the default is term signal, can also be HUP, INT, QUIT, KILL, USR1, or USR2.
Stopwaitsecs: Time to wait when the operating system sends Sigchild signals to Supervisord
Stopasgroup: If set to true, it causes Supervisor to send a stop signal to the entire process group
Killasgroup: If set to true, when sending a sigkill signal to the program, it is sent to the entire process group, and its child processes are affected.
User: If Supervisord runs with root, this setting will be used to start the subroutine
Redirect_stderr: If set to true, the process will output the standard error to the standard output file descriptor in the Supervisord background.
Stdout_logfile: Writes the standard output of the process to the file, and if Stdout_logfile is not set or set to auto, Supervisor automatically selects a file location.
Stdout_logfile_maxbytes: The standard output log file to achieve the number of automatic rotation, in kilobytes, MB, GB. If set to 0, the log file size is not restricted
Stdout_logfile_backups: Number of standard output log rotation backups, default is 10, if set to 0, do not back up
Stdout_capture_maxbytes: When the process is in stderr capture mode, the maximum bytes value written to the FIFO queue can be KB, MB, GB
Stdout_events_enabled: If set to True, the Process_log_stderr event is triggered when the process writes its stderr to the file descriptor
Stderr_logfile: Output The error log of the process to a file unless the Redirect_stderr parameter is set to True
Stderr_logfile_maxbytes: The error log file to achieve the number of automatic rotation, units in kilobytes, MB, GB. If set to 0, the log file size is not restricted
Stderr_logfile_backups: Number of error log rotation backups, default is 10, if set to 0, do not back up
Stderr_capture_maxbytes: When the process is in stderr capture mode, the maximum bytes value written to the FIFO queue can be KB, MB, GB
Stderr_events_enabled: If set to True, the Process_log_stderr event is triggered when the process writes its stderr to the file descriptor
Environment: A list of k/v pairs
Directory:supervisord will switch to the directory when the child process is generated
Umask: Set the umask of the process
ServerURL: Whether to allow child processes and internal HTTP service communication, if set to Auto,supervisor will automatically construct a URL
eg

The code is as follows:

[Program:cat]
Command=/bin/cat
process_name=% (program_name) s
Numprocs=1
directory=/tmp
Umask =022
priority=999
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0,2
Stopsignal=term
stopwaitsecs=10
user=chrism
Redirect_stderr=false
Stdout_logfile=/a/path
STDOUT_LOGFILE_MAXBYTES=1MB
stdout_logfile_backups=10
STDOUT_CAPTURE_MAXBYTES=1MB
stderr_logfile=/a/ Path
STDERR_LOGFILE_MAXBYTES=1MB
stderr_logfile_backups=10
STDERR_CAPTURE_MAXBYTES=1MB
Environment =a= "1", b= "2"
Serverurl=auto

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.