Get your services running stably by Supervisor

Introduction

Supervisor, a powerful task management software. As its name indicated, it is a supervisor which run as a daemon and monitor the status of other programs. If a program under its monitor is down, Supervisor will try to bring it back immediately.

It is developed to be a very flexible and powerful software, and it was divided into two parts: Client and Server. You can choose from multiple controlling methods, such as Unix socket, HTTP, and Supervisorctl(as its client).

Installation

Programmed in Python, Supervisor has been indexed by Pip. You can simply use pip to install it.

pip install supervisor

For Ubuntu Operating System, you can also run apt-get install supervisor

Configuring

After installation, time to configure.

First, we will need it omit its default config file. (Well, in fact, if you are using apt-get to install Supervisor, you can skip.)

echo_supervisord_conf > /etc/supervisord.conf

And let’s edit it to enable it monitoring programs for us.

vi /etc/supervisord.conf

In the config file, we can see multiple settings such as Unix socket files, log settings. And at the bottom, the [include] section.

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

Remove the semicolon, and edit the directory as you wish. To fit the Linux style, you can also change the *.ini to *.conf 😀

My config:

[include]

files = /etc/supervisor/*.conf

And then create your config for the first program.

/etc/supervisor/sample.conf

[program:usercenter]
directory = /home/gaukas/sample  ;The directory of the program
command = python ./server.py  ;Start command of the program
autostart = true  ;Auto start when supervisord start
startsecs = 5  ;If not exit in $startsecs seconds, regard it is a successful start
autorestart = true  ;Auto restart if program unexpectedly exit
startretries = 3  ;Auto restart for $startretries times
user = gaukas  ;use which user to start the program?
redirect_stderr = true  ;output error message to stdout
stdout_logfile_maxbytes = 20MB  ;stdout log file maximum size.
stdout_logfile_backups = 20  ;stdout backups
stdout_logfile = /data/logs/stdout.log  ;stdout file path (file will be created automatically, but the directory won't)

Add to system start-up (systemd)

If you are using apt-get to install supervisor, you don’t need to do anything.

/usr/lib/systemd/system/supervisord.service

[Unit] 
Description=Supervisor daemon 
[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf 
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown 
ExecReload=/usr/bin/supervisorctl $OPTIONS reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s 

[Install] 
WantedBy=multi-user.target

Enable autostart by using the following command:

systemctl enable supervisord.service

Start the process of supervisor:

systemctl start supervisord.service

After making changes to config files, reload supervisor:

systemctl reload supervisord.service

Credits

https://www.jianshu.com/p/e1c3e6fbae80

http://liyangliang.me/posts/2015/06/using-supervisor/

Leave a Reply

Your email address will not be published. Required fields are marked *