Process management Tools Supervisor

Source: Internet
Author: User

Brief introduction

Supervisor is a process management tool written in Python, and when the process terminates unexpectedly or the server goes down, hopefully the process will run automatically, Supervisord can do it well for us.

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.

At the same time, Supervisord comes with the Web monitoring interface, which can be viewed and manipulated by the browser.

Official Document: Http://www.supervisord.org/installing.html

Principle:

The Supervisor management process is the fork/exec of these managed processes as supervisor child processes. When a child process hangs, the parent process can get exactly what the child process is hanging out.

Installation

1. Python--version && pip--version

2, Easy_install Supervisor

3, echo_supervisord_conf && echo_supervisord_conf >/etc/supervisord.conf

Component

    1. Supervisord

      Supervisord is a service-side program for supervisor.

      Job: Start the Supervisor program itself, start a supervisor-managed subprocess, respond to requests from clients, restart a flashback or an abnormally exited subprocess, log the stderr or stdout of the process to the journal file, generate and process the event

    2. Supervisorctl

      This thing is still a bit of use, if say Supervisord is Supervisor Service End program, then SUPERVISORCTL is client terminal program. Supervisorctl has a shell-type command-line interface that we can use to view child process status, start/stop/restart child processes, get a list of running child processes, etc... The best thing is, Supervisorctl can not only connect to the Supervisord on the machine, but also connect to the remote Supervisord, of course, on this machine is connected through a UNIX socket, remote is connected through a TCP socket.    The communication between Supervisorctl and Supervisord is accomplished through XML_RPC. The corresponding configuration in the [SUPERVISORCTL] Block

    3. Web Server

      Web server can primarily manage processes on the interface, Web server is actually implemented through XML_RPC, can request data to supervisor, and can control supervisor and child processes. Configuration in [Inet_http_server] Block

Configuration Instructions

Start Stop command

Supervisord default to Daemon daemon mode in the background boot

Supervisord-c/etc/supervisord.conf

Stop it

Ps-ef | grep Supervisord

Kill-9

The plus-n option can be started in the foreground, which is useful for debugging startup problems.

Supervisord-n-c/etc/supervisord.conf

Program Configuration

We have already run the Supervisrod, and now we can add the configuration file of the process we want to manage. These configurations can be written in the supervisord.conf file, if the application is many, it is best to include different programs (groups) in different configuration files.
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:

[include]
Files =/etc/supervisor/*.conf

Suppose there is a user system usercenter developed with Flask, and the production environment is run using Gunicorn. The project code is located in the/home/leon/projects/usercenter,wsgi object located in wsgi.py. The way to start at the command line is this:
Cd/home/leon/projects/usercenter
Gunicorn-w 8-b 0.0.0.0:17510 Wsgi:app

The corresponding configuration file may be:

[Program:usercenter]
directory =/home/leon/projects/usercenter; Startup directory of the program
Command = gunicorn-w 8-b 0.0.0.0:17510 wsgi:app; Start command
Autostart = true; It starts automatically when the Supervisord is started.
Startsecs = 5; No exception exits after 5 Seconds of startup, as if it had started normally.
AutoRestart = true; Automatic restart after program exits unexpectedly
Startretries = 3; Startup failed auto Retry number, default is 3
user = Leon; With which user to start
Redirect_stderr = true; REDIRECT stderr to stdout, default false
Stdout_logfile_maxbytes = 20MB; StdOut log file size, default 50MB
Stdout_logfile_backups = 20; StdOut number of log file backups
; StdOut log file, 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

where the usercenter in [Program:usercenter] is the unique identity of the application and cannot be duplicated. All operations on the program (start, restart, etc.) are implemented by name.

Tips 1:python Environment
There are two ways to specify the Python environment that the program uses:
command uses an absolute path. Assuming Pyenv is used to manage the Python environment, the Gunicorn path in the example above can be replaced with/home/leon/.pyenv/versions/usercenter/bin/gunicorn. This way at a glance, recommended.
Configure PYTHONPATH with environment. Environment=pythonpath= $PYTHONPATH:/home/leon/.pyenv/versions/usercenter/bin/. Environment This configuration item is very useful and can be used to pass an environment variable to the program.

Tips 2: Background processes
Supervisor can only manage programs that run in the foreground, so if the application has options to run in the background, it needs to be closed.

Tips 3: Child processes
Sometimes a Supervisor-managed program has child processes (such as Tornado), and if only the main process is killed, the child process can become an orphan process. These two configurations ensure that all child processes are properly stopped:
Stopasgroup=true
Killasgroup=true

Command-line tools Supervisorctl

Status # View program status
Stop Program_name # closes the specified program
Start Program_name # starts the specified program
Restart Program_name # Restart the specified program
Tail-f program_name # View the log of the program
Update # Restart config file modified program (modified configuration, load new configuration with this command)

You can also operate directly from the shell command:

Supervisorctl status
SUPERVISORCTL Update

Web interface

[Inet_http_server]
port=192.168.255.124:9001




Process management Tools Supervisor

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.