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

How to install and use Supervisor under Linux

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you how to install and use Supervisor under Linux. The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn how to install and use Supervisor under Linux.

Supervisor is a general process management program developed in Python, which can turn an ordinary command-line process into a background daemon, monitor the process status, and automatically restart when exiting abnormally.

installation

Supervisor is developed based on python. Before installing Supervisor, you need to install python first. Supervisor can be installed by pip or easy_install.

Install $ yum install python-setuptools$ easy_install supervisor via easy_install

Or, install via pip

$ pip install supervisor structure Supervisor

The main process is responsible for managing the server of the process. It will create a specified number of child processes of the application according to the configuration file, manage the entire life cycle of the child process, restart the crash process, and send event notifications for process changes. Built-in web server and XML-RPC interface make process management easy.

Supervisorctl

Management client, users send messages to supervisor through the command line, you can view process status, load configuration files, start and stop processes, view process standard output and error output, remote operations, etc.

Web server

Superviosr provides web server functionality, allowing you to control processes via the web.

XML-RPC interface

XML-RPC interface provides XML-RPC services to manage and monitor child processes.

basic use

The Supervisor configuration file is named supervisord.conf and provides configuration option settings for supervisord(the Supervisor's main service command) and supervisorctl(the Supervisor's supervisory command). The Supervisor does not specify where the configuration file supervisord.conf is stored. Supervisor service starts at:

$CWD/supervisord.conf$CWD/etc/supervisord.conf/etc/supervisord.conf

These directory locations look for the configuration file supervisor.conf, and supervisors installed with yum generate the/etc/supervisor.conf configuration file by default. Supervisor also provides parameter "-c" to specify directory path of configuration file.

If you are a build installer, or on another system, you can view Supervisor's default configuration by typing "echo_supervisor_conf" in the terminal. It can be used to generate a default profile:

$ echo_supervisord_conf > /etc/supervisord.conf

With the Supervisor profile generated, it is now possible to add profiles for the processes we want to manage. It is possible to write all configuration items to the supervisord.conf file, but this is not recommended. Instead, write different programs (groups) to different configuration files by including them. To ensure that your supervisord.conf configuration file includes your custom configuration file, the following is included:

[include]files = /etc/supervisord.d/*.conf

Let's modify the configuration file to pull up the memcached process as deamon (the memcached process can also be a shell or python script) and monitor it.

First create a configuration file in the supervisor directory, as follows

$ cat /etc/supervisor.d/memcached.conf[program:memcached]#Command to start the program;command = /usr/bin/memcached -m 5120 -p 11211 -u nobody -l 0.0.0.0-b 65535#Automatic startup when supervisor starts;autostart = true#Automatic restart after program exits abnormally; autostart = true#If there is no abnormal exit after 5 seconds of startup, it is deemed to have started normally;startsecs = 5#Automatic retry times for startup failure, default is 3;startretries = 3#user who started the program;user = nobody#Redirect stderr to stdout, default false; redirect_stderr = true#standard log output;stdout_logfile=/data/log/memcached/out-memcache.log#error log output;stderr_logfile=/data/log/memcached/err-memcache.log#standard log file size, default 50MB;stdout_logfile_maxbytes = 20MB #standard log file backups;stdout_logfile_backups = 20

A configuration file requires at least one [program:x] configuration that tells supervisor which process to manage. The x in the [program:x] syntax represents the program name, which will be displayed on the client (supervisorctl or web interface). In supervisorctl, this value is used to start, restart, stop, etc. the program. The folder where the log files will be stored should be created.

A bigger advantage of using supervisor is that you can quickly start multiple processes, and the configuration parameters are as follows:

process_name=%(process_num)snumprocs=3

Indicates that 3 threads are open for the same configuration.

Starting supervisor will pull memcached process.

$ supervisord -c /etc/supervisor/supervisord.conf

ps:Supervisor will generate supervisor.log, supervisor. pid and supervisor.sock files in/tmp directory after startup. If there is any problem, you can check the log.

Check to see if Supervisor is started

$ ps -ef | grep supervisor | grep -v greproot 1170 1 0 18:57 ? 00:00:00 /usr/bin/python /usr/bin/supervisord

See if the business process has been pulled

$ supervisorctl statusmemcached RUNNING pid 1230, uptime 0:04:39

Stop Supervisor (child processes are also stopped, and start, update, restart, and stop operations can also be performed on individual programs)

$ supervisorctl shutdown

Using the supervisorctl command

Supervisor can be managed via the maintenance command supervisorctl or via the web administration interface. The maintenance command supervisorctl has two uses. One is imperative and one is interactive.

imperative

\1. Query the running status of each process

supervisorctl status

\2. Start, stop and restart the business process. Memcached is the process name, i.e. the value configured in [program:memcached].

supervisorctl start memcachedsupervisorctl stop memcachedsupervisorctl restart memcached

\3. Restart all processes belonging to the group named groupworker

supervisorctl start groupworkersupervisorctl stop groupworkersupervisorctl restart groupworker

\4. Start, stop and restart all processes (no latest configuration files loaded)

supervisorctl start allsupervisorctl stop allsupervisorctl restart all

\5. Reload the configuration file, stop the original process and start all processes according to the new configuration (note: all processes will stop and restart, online operation carefully)

supervisorctl reload

\6. According to the latest configuration file, start the process with new configuration or changes, and the process with unchanged configuration will not be affected and restarted (note: this is the command that can be operated online, and the original process will not be restarted)

supervisorctl update

Note: The process that shows the state as stop will not automatically restart with reload or update.

interactive

$ supervisorctlmemcached RUNNING pid 1256, uptime 0:01:47supervisor> stop memcachedmemcached: stoppedsupervisor> start memcachedmemcached: startedsupervisor> statusmemcached RUNNING pid 1258, uptime 0:00:04supervisor> restart memcachedmemcached: stoppedmemcached: startedsupervisor> statusmemcached RUNNING pid 1259, uptime 0:00:02supervisor>

fault handling

These faults are the pits I encountered when using supervisor. I'll fill them in for you.

Supervisor is more suitable for monitoring business applications, and can only monitor foreground programs. If your program is started as a daemon, then execute: supervisor status will prompt: BACKOFF Existed too quickly (process log may have details). For example: memcached starts with the-d option, which means it starts with a background daemon, and you can't use supervisor monitoring.

/usr/bin/memcached -d -m 5120 -p 11211 -u nobody -l 0.0.0.0 -b 65535

Another thing to note is that if the supervisor status error is: FATAL Exited too quickly (process log may have details), check whether the user = nobody is added to cause the execution permission problem.

If this error occurs:

Error: .ini file does not include supervisord section

For help, use /usr/bin/supervisord -h

There is a problem with the format of your custom program configuration file (/etc/supervisor/conf.d/*.conf), so check it carefully.

other

1) You can write your own script to add Supervisor to chkconfig and start it automatically with the system. Or you can use an off-the-shelf script: Supervisor init scripts.

2) In addition to supervisorctl, you can also configure supervirod to launch the web management interface, which uses Basic Auth for identity authentication.

3) In addition to the control of a single process, you can also configure groups for group management. Check the log files frequently, including the log of supervisor and the log files of each pragram. Half of the information that the program crashes or throws exceptions will be output to stderr. You can check the corresponding log files to find problems.

4) Supervisor has a lot of functions, there are many other configurations, you can get more information in the official document: supervisord.org/index.html

The above is "How to install and use Supervisor under Linux" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

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

Development

Wechat

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

12
Report