Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Installation, configuration and basic usage of Supervisor under Linux

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the installation configuration and basic use of Supervisor under Linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the installation configuration and basic use of Supervisor under Linux".

Supervisor (http://supervisord.org) is a process management tool written in Python that can be easily used to start, restart, and shut down processes (not just Python processes). In addition to the control of a single process, you can also start and shut down multiple processes at the same time, for example, if an unfortunate server problem causes all applications to be killed, you can use supervisor to start all applications at the same time instead of typing commands one by one.

Installation

Supervisor can run on Linux and Mac OS X. As mentioned earlier, supervisor is written in Python, so it's easy to install, and you can use pip directly:

The code is as follows:

Sudo pip install supervisor

If it is a Ubuntu system, you can also install it using apt-get.

Supervisord configuration

Supervisor is quite powerful and provides a wealth of features, but we may only need to use a few of them. After the installation is complete, you can write a configuration file to meet your needs. For convenience, we split the configuration into two parts: supervisord (supervisor is a program based on the C supervisor S model, which is the server side, corresponding to the client side: supervisorctl) and the application (that is, the program we want to manage).

First, take a look at the configuration file for supervisord. After installing supervisor, you can run the echo_supervisord_conf command to output the default configuration items, or you can redirect to a configuration file:

The code is as follows:

Echo_supervisord_conf > / etc/supervisord.conf

Excluding most of the comments and "irrelevant" parts, we can look at these configurations first:

[unix_http_server]

File=/tmp/supervisor.sock; UNIX socket file, supervisorctl will use the

; chmod=0700; mode of socket file. Default is 0700.

; chown=nobody:nogroup; owner of the socket file, format: uid:gid

; [inet_http_server]; HTTP server, which provides web management interface

; port=127.0.0.1:9001; Web manages the IP and ports running at the backend. If it is open to the public network, you need to pay attention to security.

; username=user; login to the user name of the administrative backend

; password=123; password for logging in to the administrative backend

[supervisord]

Logfile=/tmp/supervisord.log; log file. Default is $CWD/supervisord.log.

Logfile_maxbytes=50MB; log file size, which exceeds rotate. Default is 50MB.

Logfile_backups=10; the number of reserved backups of log files defaults to 10

Loglevel=info; log level, default info, other: debug,warn,trace

Pidfile=/tmp/supervisord.pid; pid file

Nodaemon=false; whether to start in the foreground. The default is false, that is, it starts in the way of daemon.

Minfds=1024; the minimum value of the file descriptor that can be opened. Default is 1024.

Minprocs=200; the minimum number of processes that can be opened. Default is 200.

; the below section must remain in the config file for RPC

; (supervisorctl/web interface) to work, additional interfaces may be

; added by defining them in separate rpcinterface: sections

[rpcinterface:supervisor]

Supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

Serverurl=unix:///tmp/supervisor.sock; connect to supervisord through UNIX socket, and the path is the same as that of file in unix_http_server

; serverurl= http://127.0.0.1:9001; connect to supervisord through HTTP

; contains other configuration files

[include]

Files = relative/directory/*.ini; can be * .conf or * .ini

[unix_http_server]

File=/tmp/supervisor.sock; UNIX socket file, supervisorctl will use the

; chmod=0700; mode of socket file. Default is 0700.

; chown=nobody:nogroup; owner of the socket file, format: uid:gid

; [inet_http_server]; HTTP server, which provides web management interface

; port=127.0.0.1:9001; Web manages the IP and ports running at the backend. If it is open to the public network, you need to pay attention to security.

; username=user; login to the user name of the administrative backend

; password=123; password for logging in to the administrative backend

[supervisord]

Logfile=/tmp/supervisord.log; log file. Default is $CWD/supervisord.log.

Logfile_maxbytes=50MB; log file size, which exceeds rotate. Default is 50MB.

Logfile_backups=10; the number of reserved backups of log files defaults to 10

Loglevel=info; log level, default info, other: debug,warn,trace

Pidfile=/tmp/supervisord.pid; pid file

Nodaemon=false; whether to start in the foreground. The default is false, that is, it starts in the way of daemon.

Minfds=1024; the minimum value of the file descriptor that can be opened. Default is 1024.

Minprocs=200; the minimum number of processes that can be opened. Default is 200.

; the below section must remain in the config file for RPC

; (supervisorctl/web interface) to work, additional interfaces may be

; added by defining them in separate rpcinterface: sections

[rpcinterface:supervisor]

Supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

Serverurl=unix:///tmp/supervisor.sock; connect to supervisord through UNIX socket, and the path is the same as that of file in unix_http_server

; serverurl= http://127.0.0.1:9001; connect to supervisord through HTTP

; contains other configuration files

[include]

Files = relative/directory/*.ini; can be * .conf or * .ini

We save the above part of the configuration to / etc/supervisord.conf (or any other file with access), and then start supervisord (specify the configuration file path through the-c option, and if not specified, the configuration files will be found in this order: $CWD/supervisord.conf, $CWD/etc/supervisord.conf, / etc/supervisord.conf):

The code is as follows:

Supervisord-c / etc/supervisord.conf

Program configuration

Now that we have supervisrod up and running, we can add the configuration file of the process we want to manage. All configuration items can be written into supervisord.conf files, but this is not recommended. Instead, different programs (groups) can be written into different configuration files in the way of include.

To give an example, let's create a new directory / etc/supervisor/ to store these configuration files, and modify the configuration in the include section of / etc/supervisord.conf accordingly:

The code is as follows:

[include]

Files = / etc/supervisor/*.conf

Suppose you have a user-centric system written in the Python and Flask frameworks, named usercenter, and using gunicorn (http://gunicorn.org/) as the web server). The project code located in the / home/leon/projects/usercenter,gunicorn configuration file is gunicorn.py,WSGI callable, which is the app attribute in wsgi.py. So the way to start directly on the command line might be like this:

The code is as follows:

Cd / home/leon/projects/usercenter

Gunicorn-c gunicorn.py wsgi:app

Now write a configuration file to manage the process (note: when managing with supervisord, the daemon option for gunicorn needs to be set to False):

[program:usercenter]

Directory = / home/leon/projects/usercenter; the startup directory of the program

Command = gunicorn-c gunicorn.py wsgi:app; start the command, you can see that it is the same as the command started manually on the command line

Autostart = true; also starts automatically when supervisord starts

Startsecs = 5; if there is no abnormal exit 5 seconds after startup, it is deemed to have been started normally.

Autorestart = true; automatically restart after program exits abnormally

Startretries = 3; failed startup automatically retries. 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; number of stdout log file backups

; stdout log file, it should be noted that it cannot be started normally when the specified directory does not exist, so you need to create a directory manually (supervisord will automatically create log files)

Stdout_logfile = / data/logs/usercenter_stdout.log

You can add the environment variables you need through environment, and a common use is to modify PYTHONPATH

; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere

[program:usercenter]

Directory = / home/leon/projects/usercenter; the startup directory of the program

Command = gunicorn-c gunicorn.py wsgi:app; start the command, you can see that it is the same as the command started manually on the command line

Autostart = true; also starts automatically when supervisord starts

Startsecs = 5; if there is no abnormal exit 5 seconds after startup, it is deemed to have been started normally.

Autorestart = true; automatically restart after program exits abnormally

Startretries = 3; failed startup automatically retries. 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; number of stdout log file backups

; stdout log file, it should be noted that it cannot be started normally when the specified directory does not exist, so you need to create a directory manually (supervisord will automatically create log files)

Stdout_logfile = / data/logs/usercenter_stdout.log

You can add the environment variables you need through environment, and a common use is to modify PYTHONPATH

; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere

A configuration file requires at least one configuration in the [program:x] section to tell supervisord which process to manage. The x in the [program:x] syntax represents program name, which is displayed on the client side (supervisorctl or web interface). This value is used to perform start, restart, stop and other operations on the program in supervisorctl.

Supervisorctl subcommand

The code is as follows:

$supervisorctl help

Default commands (type help):

= =

Add clear fg open quit remove restart start stop update

Avail exit maintail pid reload reread shutdown status tail version

Use supervisorctl

Supervisorctl is a command line client tool for supervisord. You need to specify to use the same configuration file as supervisord at startup, otherwise look for configuration files in the same order as supervisord.

The code is as follows:

Supervisorctl-c / etc/supervisord.conf

The above command will enter the shell interface of supervisorctl, and then you can execute different commands:

The code is as follows:

> status # View program status

> stop usercenter # close the usercenter program

> start usercenter # start the usercenter program

> restart usercenter # restart usercenter program

> reread # reads the configuration file with updates (additions) and will not start the newly added program

> update # restart the program modified by the configuration file

All of the above commands have corresponding output. In addition to entering the shell interface of supervisorctl, you can also run them directly on the bash terminal:

The code is as follows:

$supervisorctl status

$supervisorctl stop usercenter

$supervisorctl start usercenter

$supervisorctl restart usercenter

$supervisorctl reread

$supervisorctl update

Control daemon

Each time you modify the main configuration file or add a subprofile, you need to perform a supervisorctl update for the new configuration to take effect:

The code is as follows:

Sudo supervisorctl update

Control the daemon:

The code is as follows:

# Control all processes

Sudo supervisorctl start all

Sudo supervisorctl stop all

Sudo supervisorctl restart all

# directed control of specified process

Sudo supervisorctl stop iot-kb

Sudo supervisorctl start iot-kb

Sudo supervisorctl restart iot-kb

At this point, I believe you have a deeper understanding of the "installation configuration and basic use of Supervisor under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report