Children sing songs in front of the door
Sunlight it warms the river
Today, I have studied the Supervisord. This is a Linux process management tool. It is developed in Python and is mainly used to maintain processes in the background (similar to the master daemon). It can monitor the status of the process, automatically restart the process, etc. Maintenance and monitoring of services.
Since it is developed in python, it is most convenient to use pip installation.
Description: After the installation is complete, there are three more tools: echo_supervisord_conf, supervisorctl, and supervisord.
First, you can use the echo_supervisord_conf command to get the supervisor configuration template:
Description: This command creates a configuration file named supervisord.conf in the current directory, and edits the configuration file:
Take a look at the main configuration items in the default configuration file: (There are some configurations that are not commonly used and can be ignored)
Run the following command to start the supervisord process to test whether the supervisord installation is successful and executed.
Check if there is one more supervisord in the system process:
The program is used to configure and monitor different application processes. It is recommended that each application write a separate program configuration file, and then load all application configurations through include in supervisord.conf.
Here is an example of creating a celery process, first written in the last line of supervisord.conf:
Then create the /etc/supervisor directory and create the /etc/supervisor/celery_touchscan.conf file in the directory and write:
Restart the supervisord process:
At this point, check the process on the system and find that a supervisord daemon is created, and 10 celery work processes (the number of celery work processes depends on the -c parameter in the command command and the numprocs parameter in the configuration file. The numprocs parameter refers to the operation. Several command commands, and the number of jobs that need to be run is specified in the celery command line)
Note: If you manually kill the celery worker process, you will find that the celery worker process will be automatically restarted by supervisord. Only when the supervisord daemon is killed can you actually kill the celery worker process.
The effect is as follows:
Each column represents: programe name, process name, process status, process id, run time
Description: You can execute directly in the system shell, or you can execute supervisorctl first and enter supervisorvl_shell to execute the corresponding command.
If the project uses the pyenv module of python to set the environment, the path to the python environment needs to be specified in the supervisord configuration file. There are two ways to specify the Python environment that the program uses:
- command uses an absolute path.
- Configure PYTHONPATH via environment.
Sometimes a program hosted with Supervisor will have child processes. If only the main process is killed, the child process may become an orphan process. Ensure that all child processes are properly stopped with these two configurations:
After modifying the supervisord configuration file, you need to restart the supervisord process.
Supervisor can only manage programs that run in the foreground, so if the application has an option to run in the background, it needs to be closed.
Supervised is mainly used to manage processes, not scheduled tasks, so if there are requirements for timed tasks, use them in conjunction with crontab. Of course, if you are managing the celery service, you can combine the celery’s own timed task function, which can be moved: https://thief.one/2017/08/25/1/
The previous introduction is to use the supervisord management process locally, so how to implement the process on the remote management server? The supervisord tool provides related APIs. First you need to open the relevant configuration information in the configuration file:
Then start supervisord, you can use the web interface to manage the process, open http://127.0.0.1:9001. Of course, the rpc interface is also provided, which can be called remotely. For example, the code is as follows:
The api operation is relatively simple. The specific method can be found in the documentation: http://supervisord.org/api.html#xml-rpc