search by tags

for the user

adventures into the land of the command line

daemonising multiple gunicorns with supervisor

if you’ve just started using gunicorn, you might have noticed you can’t run it as a daemon out of the box. so how do people do it? one way of doing it is using a process control system called supervisor

$ pip install supervisor

supervisor can manage and daemonise not just gunicorn, but other things as well. it has a config file here

$ vim /etc/supervisor.conf

inside it you can put your instructions. you need a [supervisord] section, and at least one [program:your_funky_app_name] section, but you can put as many as you want

[supervisord]
nodaemon=false
logfile=/var/log/supervisor/supervisord.log
loglevel=error
logfile_maxbytes=1MB
logfile_backups=4

[program:my_funky_app]
command=gunicorn -w 10 -k eventlet -b 0.0.0.0:5001 index:app
stdout_logfile=/var/log/gunicorn/gunicorn_myapp1.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=4
redirect_stderr=true
directory=/var/www/myapp1/
user=myuser

[program:my_SUPER_funky_app]
command=gunicorn -w 10 -k eventlet -b 0.0.0.0:5002 index:app
stdout_logfile=/var/log/gunicorn/gunicorn_myapp2.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=4
redirect_stderr=true
directory=/var/www/myapp2/
user=myuser

it’s a good idea to run it as a non-root user and also to create make sure the logfile directories and logfiles exist already. also, setting nodaemon to true starts supervisor in the foreground instead of daemonising it.

to start supervisor

$ /usr/bin/supervisord -c /etc/supervisord.conf

if you want to stop it when it’s daemonised, you’d have to

$ ps -ef | grep gunicorn

then find the pid id and kill it

$ kill -9 12345 (or whatever it is)

in my next post i’ll talk about how to create an init script, for easily starting and stopping services that don’t come with one out of the box