Introduction to Supervisor

What is Supervisor?

A system that can control and manage processes.

Specifically, it daemonizes the process and restarts it in the event of a crash.

For example, my assigned project uses Laravel's queue worker, which is useful for continuous parallel running of programs.

Unfortunately for Windows users, it doesn't start on Windows because it's UNIX-like.

Official documentation http://supervisord.org/running.html#adding-a-program

About this article

I will write the minimum necessary contents about the basic usage of Supervisor.

Installation

This time, we will practice it on the EC2 instance launched appropriately on AWS.

Server access-superuser switching

...$ ssh -i ~/.ssh/udemy_sample2.pem [email protected]
[ec2-user@ip-172-31-37-208 ~]$ sudo su -

Install supervisor I haven't investigated it in detail, but it seems that ʻeasy_install` is good.

[root@ip-172-31-37-208 ~]# sudo easy_install supervisor
Searching for supervisor
Reading https://pypi.python.org/simple/supervisor/
Downloading https://files.pythonhosted.org/packages/ca/1f/07713b0e1e34c312450878801d496bce8b9eff5ea9e70d41ff4e299b2df5/supervisor-4.1.0-py2.py3-none-any.whl#sha256=a76b2f77a560f2dc411c0254a4eb15f555e99faac48621b0f1fc9ab013944f47
Best match: supervisor 4.1.0
Processing supervisor-4.1.0-py2.py3-none-any.whl
Installing supervisor-4.1.0-py2.py3-none-any.whl to /usr/lib/python2.7/site-packages
writing requirements to /usr/lib/python2.7/site-packages/supervisor-4.1.0-py2.7.egg/EGG-INFO/requires.txt
Adding supervisor 4.1.0 to easy-install.pth file
Installing echo_supervisord_conf script to /usr/bin
Installing pidproxy script to /usr/bin
Installing supervisorctl script to /usr/bin
Installing supervisord script to /usr/bin

Installed /usr/lib/python2.7/site-packages/supervisor-4.1.0-py2.7.egg
Processing dependencies for supervisor
Finished processing dependencies for supervisor

Creating a configuration file

[root@ip-172-31-37-208 ~]# echo_supervisord_conf > /etc/supervisord.conf

The configuration file is placed under etc.

Addition of program to be daemonized (when not managed individually)

If you do not manage the configuration files individually, add the program you want to daemonize to supervisord.conf as follows.

supervisord.conf


[program:foo]
command=/bin/cat

Addition of program to be daemonized (when managing individually)

If you want to manage the configuration files individually, follow the procedure below.

Creating a directory for storing individual program configuration files

[root@ip-172-31-37-208 ~]# mkdir /etc/supervisord.d

Modify supervisord.conf

Changes (before change)

supervisord.conf


;[include]
;files = relative/directory/*.ini

Changed part (after change)

supervisord.conf


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

Now, the individual configuration files created under /etc/supervisord.d will also be read.

Creating individual program configuration files

[root@ip-172-31-37-208 ~]# vi /etc/supervisord.d/foo.conf

Created as follows

foo.conf


[program:foo]
command=/bin/cat

About the command to use

supervisord

Used to start the supervisor.

It is not used for detailed program operations.

supervisorctl

Used in the operation of the executed program. See the URL below for detailed operation methods.

Start Supervisor ~ Run Program

Start Supervisor.

[root@ip-172-31-37-208 ~]# /usr/bin/supervisord -c /etc/supervisord.conf

It will start with just / usr / bin / supervisord, but in that case it seems better to use the c option and specify the absolute path of the configuration file because it goes to search the location of the configuration file in order. ..

Check the program in the configuration file.

[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl status
foo                              RUNNING   pid 29747, uptime 0:02:30

The program described in the configuration file seems to be executed automatically when Supervisor starts.

Try kill.

[root@ip-172-31-37-208 etc]# kill -9 29747
[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl status
foo                              RUNNING   pid 29761, uptime 0:00:03

Confirm that the process ID has changed and it has been restarted.

Try stopping the process.

[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl stop foo
foo: stopped
[root@ip-172-31-37-208 ~]# /usr/bin/supervisorctl status
foo                              STOPPED   Mar 29 01:17 AM

Make sure the process is stopped.

Try deleting the process.

[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl remove foo
foo: removed process group
[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl status
[root@ip-172-31-37-208 etc]#

Make sure the process is dead.

Add a new program and try running it in the foreground.

Add the following to supervisord.conf

supervisord.conf


[program:foo2]
command=watch -n 1 `date`

Read the configuration file and start the process

[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl reread
foo: available
foo2: available
[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl add foo2
foo2: added process group
[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl status
foo2                             RUNNING   pid 1177, uptime 0:00:04
[root@ip-172-31-37-208 etc]# /usr/bin/supervisorctl fg foo2
Every 1.0s: `date`                                      Sun Mar 29 04:24:25 2020

sh: Sun: command not found

Confirm execution in the foreground.

You can exit with Ctl + C. (Of course the process remains daemonized)

Command list

I have extracted the ones that I think I will use often.

status

Check the status of the process status.

reread

Read the configuration file.

No programs are added or removed at this stage.

add <name>

Apply the program described in the configuration file (process start).

remove <name>

Delete a stopped process.

restart <name>

Process restart.

Note that this alone will not read the contents of the configuration file.

stop

Stop the process.

start

Start the process.

update

Reload the configuration file and restart the program.

Is only the program whose settings have changed restarted?

reload

Restart the Supervisor daemon.

Summary

Convenient! !! !! !! !!

Recommended Posts

Introduction to Supervisor
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Tkinter 1: Introduction
Introduction to PyQt
Introduction to Scrapy (2)
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
Introduction to Lightning pytorch
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to PyQt4 Part 1
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
Introduction to Monte Carlo Method
[Learning memorandum] Introduction to vim
Introduction to PyTorch (1) Automatic differentiation
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
Introduction to Cython Writing [Notes]
An introduction to private TensorFlow
Kubernetes Scheduler Introduction to Homebrew
An introduction to machine learning
[Introduction to cx_Oracle] Overview of cx_Oracle
A super introduction to Linux
Introduction
AOJ Introduction to Programming Topic # 7, Topic # 8
[Introduction to pytorch-lightning] First Lit ♬
Introduction to Anomaly Detection 1 Basics
Introduction to RDB with sqlalchemy Ⅰ
[Introduction to Systre] Fibonacci Retracement ♬
Introduction to Nonlinear Optimization (I)
Introduction to serial communication [Python]
AOJ Introduction to Programming Topic # 5, Topic # 6
Introduction to Deep Learning ~ Learning Rules ~
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
[Introduction to cx_Oracle] (8th) cx_Oracle 8.0 release
Introduction to discord.py (3) Using voice
An introduction to Bayesian optimization
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
Super introduction to machine learning
Introduction to Ansible Part ③'Inventory'
Series: Introduction to cx_Oracle Contents
[Introduction] How to use open3d
Introduction to Python For, While
Introduction to Deep Learning ~ Backpropagation ~
Introduction to Ansible Part ④'Variable'
Introduction to vi command (memorandum)
Introduction to Linux Commands ~ LS-DYNA Edition ~