Daemonize a Python web app with Supervisor

When I was developing a Python web application with aws using the Tornado framework, the web application running with aws often sleeps (the app falls), so I made it a daemon and made it work 24/7 (always started). A memo when I tried to wake up (restart the application) even if I fell asleep.

What is Supervisor?

Supervisor is a lightweight process management tool made in Python.

procedure

The procedure is as follows. All work is done on ubuntu 14.04 on aws.

Installation

Supervisor can be installed with either ʻapt-get or pip, but for the time being, I will describe how to install with ʻapt-get on Ubuntu.

$ sudo apt-get install supervisor

Confirm that it has been installed. The version seems to include 3.0b2.

$ supervisord -v
3.0b2

$ sudo supervisorctl version
3.0b2

Setting

Write the information for starting the Web application in the Supervisor setting file (conf file). Create a conf file in * /etc/supervisor/conf.d/*.

$ sudo vim /etc/supervisor/conf.d/tornado.conf

The contents are as follows. The name of the process to be managed by Supervisor is ** tornado **.

tornado.conf


[program:tornado]  ;Process name
directory=/home/ubuntu/tornado-app  ;Working directory
command=/home/ubuntu/tornado-app/venv/bin/python /home/ubuntu/tornado-app/server.py --port=3000  ;Start command
numprocs=1  ;Number of boot instances?
autostart=true  ;autostart
autorestart=true  ;Reboot automatically
user=ubuntu  ;Launch user
redirect_stderr=true  ;Output standard error
stdout_logfile=/var/log/supervisor/tornado.log  ;Log file output destination

Suppose your web app has already been git cloned to / home / ubuntu / tornado-app.

$ cd ~/tornado-app
$ source venv/bin/activate  #activate virtualenv
(venv)$ python server.py --port=3000 &

Also, please refer to Supervisor configuration for how to write the Supervisor configuration file.

Start supervisor, read configuration file

Start Supervisor using the service supervisor command

Restart Supervisor itself.

$ sudo service supervisor restart

Make sure that Supervisor itself is running.

$ sudo service supervisor status
 is running

Process startup and management

Manage the process with the supervisorctl command.

If you change the conf file, you will definitely need to reread it. Use the following command to read the conf file.

$ sudo supervisorctl reread

After that, start the web application managed by Supervisor.

$ sudo supervisorctl start tornado  #Start-up

Check if the web application is running.

$ sudo supervisorctl status  #Status check
tornado                          RUNNING    pid 3414, uptime 0:07:19

It started!

By the way, the stop and restart are as follows.

$ sudo supervisorctl stop tornado  #Stop
$ sudo supervisorctl restart tornado  #Reboot

In addition, here was helpful for the supervisorctl command.

reference

Recommended Posts

Daemonize a Python web app with Supervisor
Play like a web app with ipywidgets
[Python] A quick web application with Bottle!
Create a simple web app with flask
Easy web app with Python + Flask + Heroku
Run a Python web application with Docker
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a web framework with Python! (2)
Daemonize a Python process
Vienna with Python + Flask web app on Jenkins
Launch a web server with Python and Flask
Extract data from a web page with Python
Deploy a Python 3.6 / Django / Postgres web app on Azure
Web scraping with python + JupyterLab
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
I made a net news notification app with Python
Launch a Python web application with Nginx + Gunicorn with Docker
Web API with Python + Falcon
Make a fortune with Python
Write code to Unit Test a Python web app
Web application with Python + Flask ② ③
Web scraping beginner with python
Create a directory with python
Deploy a real-time web app with swampdragon x apache
Streamline web search with python
Deploy a web app created with Streamlit to Heroku
Web application with Python + Flask ④
How to deploy a web app made with Flask to Heroku
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
Embed a Python interpreter into a C ++ app with pybind11 + cmake
(Failure) Deploy a web app made with Flask on heroku
Easy deep learning web app with NNC and Python + Flask
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Let's make a GUI with python.
Getting Started with Python Web Applications
Solve ABC166 A ~ D with Python
Web scraping with Python First step
I tried web scraping with python.
Monitor Python web apps with Prometheus
Create a virtual environment with Python!
Get web screen capture with python
I made a fortune with Python.
Building a virtual environment with Python 3
Creating a simple app with flask
Solve ABC168 A ~ C with Python
Make a recommender system with python
Build a web application with Django
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
If you know Python, you can make a web application with Django
[python, ruby] fetch the contents of a web page with selenium-webdriver