running-small-python-apps-in-supervisor

2017-11-06

Quick Start

Overview

So supervisor helps you keep small apps running and makes it easy to monitor via a web interface. So it’s perfect for keeping like a slack bot up or other small python script running on an instance.

You can monitor console output without having to log in and restart if you want to.
To setup a app on supervisor just need add config file to supervisors conf directory.(use my template below)

Installation and other usefull commands

1
2
3
4
5
6
7
8
9
10
#installs supervisor
sudo apt-get install supervisor
# Will restart supervisor. Note all apps its running will restart
sudo service supervisor restart
#will reload supervisor apps configs
sudo supervisorctl reread
sudo supervisorctl update

Master Config for supervisor

The main config file for supervisor is in /etc/supervisor/supervisord.conf

1
2
3
4
5
6
7
[inet_http_server]
port = 9001
username = admin
password = password123
[include]
files = /etc/supervisor/conf.d/*.conf

Ive added the following to the config. its enables the cool web console on port 9001.
Which is nice dashboard for monitoring.
Would recommend running this behind firewall for securty reasons and change password.

Then lastly you can include configs in the conf.d directory.
This enable to just add .conf files to that folder and it will start the apps.

Config template

location /etc/supervisor/conf.d/

1
2
3
4
5
6
7
[program:app_name1]
directory=/home/app_name1/
command=python3 -u app1.py
startsecs=10
priority=1
autostart=true
autorestart=true

You can place multiple apps in a config file. To load run load command above or restart service.
As you can see if application terminates the app will be restarted.
Other modifications is I specified home directory for application will run in it own directory. Usefull if app uses config files or save info locally.

Lastly Startsec adds delay on start.With this you can stagger the startup of all apps so does cause load spikes starting up.

Monitoring

You can now monitor your application via on localhost:9001

Check supervisors main page for more details
supervisor main page

The platform is also highly extensible
plugins