supervisor--Process Management Tools

Source: Internet
Author: User

Supervisor (http://supervisord.org) is a python-written process management tool that can be easily used to start, restart, and close processes (not just the Python process). In addition to controlling a single process, you can start and close multiple processes at the same time, such as unfortunate server problems that cause all applications to be killed, and you can start all applications with supervisor at the same time instead of one by one.

1. Installation

The Supervisor can be run on Linux, Mac OS x. As mentioned earlier, supervisor is written in Python, so it is easy to install and can be used directly with PIP:

sudo pip install Supervisor

If it is an Ubuntu system, you can also use the Apt-get install Supervisor installation.

configuration of 2.supervisord:

Supervisor is quite powerful and provides a lot of functionality, but we may only need to use a small part of it. After the installation is complete, you can write a configuration file to meet your needs. For convenience, we divide the configuration into two parts: Supervisord (Supervisor is a C/S model, which is the server side, corresponding to the client side: SUPERVISORCTL) and the application (i.e. the program we want to manage).

First look at the Supervisord configuration file. After installing supervisor, you can run the echo_supervisord_conf command output default configuration item or redirect to a configuration file:

Supervisor after installation, there will be a configuration file supervisord.conf
To run the command:

Echo_supervisord_conf

Output configuration file Details:

1 [Unix_http_server]2file=/tmp/Supervisor.sock; UNIX socket file, Supervisorctl will use3; chmod=0700; Socket file mode, default is 07004; chown=nobody:nogroup; socket file owner, format: Uid:gid5 6 ; [Inet_http_server]; HTTP server, providing Web management interface7;p ort=127.0.0.1:9001 ; Web Management background Run IP and port, if open to public network, need to pay attention to security8; username=User Name of login admin background9;p assword=123; login Admin background passwordTen  One [Supervisord] ALogfile=/tmp/supervisord.log; Log file, default is $CWD/Supervisord.log -logfile_maxbytes=50MB; log file size, exceeding rotate, default 50MB -logfile_backups=10; Log files retain the number of backups by default 10 theLoglevel=info, log level, default info, others: Debug,warn,trace -pidfile=/tmp/supervisord.pid; PID file -nodaemon=false, if it is started in the foreground, the default is False, which is started in daemon mode . -minfds=1024; The minimum value of the file descriptor that can be opened, default 1024 +minprocs=200; Minimum number of processes that can be opened, default 200 -  +; The below section must remaininchThe config file forRPC A; (supervisorctl/Web Interface) to work, additional interfaces at; Added by defining theminchseparate rpcinterface:sections - [Rpcinterface:supervisor] -Supervisor.rpcinterface_factory =Supervisor.rpcinterface:make_main_rpcinterface -  - [Supervisorctl] -serverurl=unix:///tmp/Supervisor.sock; connection Supervisord via UNIX socket, the path is consistent with the file of the Unix_http_server section in; serverurl=http://127.0.0.1:9001; connect to Supervisord via HTTP -  to ; contains additional configuration files + [include] -Files = Relative/directory/*.ini; It could be *.conf or *.ini.


We can also use the redirect operator to direct the configuration file to the/etc path (easy to manage)

Echo_supervisord_conf>/etc/supervisord.conf

We save this section of the configuration to/etc/supervisord.conf (or any other file that has access), and then start Supervisord (specifying the profile path with the-C option, if you do not specify that the configuration file will be found in this order: $CWD/ supervisord.conf, $CWD/etc/supervisord.conf,/etc/supervisord.conf):

1 supervisord-c/etc/supervisord.conf
3.program Configuration

We have already run the Supervisrod, and now we can add the configuration file of the process we want to manage. It is possible to write all the configuration items into the supervisord.conf file, but it is not recommended to do so, but to write different programs (groups) into different configuration files by using the Include method.

For example, we create a new directory/etc/supervisor/for these configuration files, corresponding to the/etc/supervisord.conf include section of the configuration modified:

1 [include] 2 files =/etc/supervisor/*.conf
Suppose there is a user-centric system written in Python and Flask framework, named Usercenter, with Gunicorn (http://gunicorn.org/) as a Web server. The project code is located /home/leon/projects/usercenter, the Gunicorn configuration file is gunicorn.py, WSGI callable is the app attribute in wsgi.py. So the way to start directly on the command line might be this:
1 cd/home/leon/projects/usercenter2 gunicorn-c gunicorn.py Wsgi:app

Now write a configuration file to manage this process ( Note: When managed with Supervisord, the gunicorn daemon option needs to be set to False):
1 [Program:usercenter]2Directory =/home/leon/projects/Usercenter; The startup directory of the program3Command = Gunicorn-c gunicorn.py wsgi:app; Start command, you can see that the command is started manually at the command line is the same4Autostart =true; automatically starts when the Supervisord is started5Startsecs = 5; Start 5seconds after no abnormal exit, as if it had started normally6AutoRestart =true; automatic restart after program exits unexpectedly7Startretries = 3; Startup failed auto Retry number, default is 38user =Leon; with which user to start9Redirect_stderr =true; redirect stderr to stdout, default falseTenStdout_logfile_maxbytes =20MB; stdout log file size, default 50MB OneStdout_logfile_backups = 20; stdout log file backup number A stdout log file, you need to be aware that it does not start properly when the specified directory does not exist, so you need to create the directory manually (Supervisord automatically creates the log file) -Stdout_logfile =/data/logs/Usercenter_stdout.log -   the , you can add the required environment variables through environment, a common use is to modify the PYTHONPATH -; Environment=pythonpath= $PYTHONPATH:/path/to/somewhere

A configuration file requires at least one [program:x] part of the configuration to tell Supervisord that the process needs to be managed. The [program:x] expression program name in the syntax is x displayed on the client (SUPERVISORCTL or Web interface), and in Supervisorctl, this value is passed to start, restart, stop, and so on.

4. Using Supervisorctl

Supervisorctl is a command-line client tool for Supervisord, which requires the same configuration file to be specified with Supervisord at startup, otherwise the configuration file is searched sequentially, as in Supervisord.

1 supervisorctl-c/etc/supervisord.conf

The above command will go into the Supervisorctl shell interface, and then you can execute different commands:

1 > Status    #  View program status 2 > Stop usercenter   #  Close Usercenter Program 3 > Start usercenter  #  start usercenter program 4 > restart Usercenter    # Restart usercenter program 5 > Reread    # Read the configuration file with update (add), Do not start the newly added program 6 > Update    # Restart profile Modified Program

These commands all have the corresponding output, in addition to entering the Supervisorctl shell interface, you can also run directly on the bash terminal:

1 $ supervisorctl Status 2 $ supervisorctl Stop Usercenter 3 $ supervisorctl Start Usercenter 4 $ supervisorctl Restart Usercenter 5 $ supervisorctl Reread 6 $ supervisorctl Update
5. Other

In addition to Supervisorctl, you can also configure Supervisrod to start the Web management interface, which uses Basic Auth to authenticate as a Web daemon.

In addition to the control of a single process, group can also be configured for group management.

Often view log files, including Supervisord logs and individual pragram log files, the program crash or throws the exception of the information half will be output to stderr, you can view the corresponding log file to find the problem.

Supervisor has a wealth of features and many other configurations that can be found in the official documentation: http://supervisord.org/index.html

supervisor--Process Management Tools

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.