Abhirath Mahipal Abhirath Mahipal - 10 days ago 5
Linux Question

Unable to start a Python app using Supervisord

I have a small app that I want to start using supervisord. I've tried the following



My initial shell script could start and stop celery and Flask as daemons by saving the PID in a text file. Since supervisord would take care of killing it, I got rid of the stop section and non daemonized the script.

After trial and error these are the script and conf duos that I think make sense but they don't work.

1



Shell Script

#!/bin/bash


if [[ $1 == "gunicorn" ]]
then
cd /home/abhirath/Desktop/Hitler
source env/bin/activate
python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app

elif [[ $1 == "celery" ]]
then
cd /home/abhirath/Desktop/Hitler
source env/bin/activate
python env/bin/celery -A testrunner worker --concurrency=3 --loglevel=info

else
echo "Usage:-"
echo "To start celery:-"
echo "./hitler.sh celery"
echo "To start Gunicorn"
echo "./hitler.sh gunicorn"
fi


Conf File

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=/home/abhirath/Desktop/Hitler/hitler.sh gunicorn
stderr_logfile =/home/abhirath/Desktop/supervisor.err.log
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true

[program:celery]
command=/home/abhirath/Desktop/Hitler/hitler.sh celery
stderr_logfile=/home/abhirath/Desktop/supervisor.err2.log
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true





2



Without a shell file

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=source env/bin/activate; python env/bin/gunicorn -b 0.0.0.0:3333 -w 2 gunicornserve:app;
directory=/home/abhirath/Desktop/Hitler

[program:celery]
command=source env/bin/activate; python env/bin/celery -A testrunner worker --concurrency=4 --loglevel=info;
directory=/home/abhirath/Desktop/Hitler


stderr_logfile, autostart, autorestart, stopasgroup, killasgroup same as #1


I get a message saying could not find the command source. I tried the same command on the terminal in the same directory and it works.





3



Shell Script

#!/bin/bash


if [[ $1 == "gunicorn" ]]
then
source env/bin/activate
python env/bin/gunicorn -b 0.0.0.0:3333 -w 3 gunicornserve:app

elif [[ $1 == "celery" ]]
then
source env/bin/activate
python env/bin/celery -A testrunner worker --concurrency=3 --loglevel=info

else
echo "Usage:-"
echo "To start celery:-"
echo "./hitler.sh celery"
echo "To start Gunicorn"
echo "./hitler.sh gunicorn"
fi


Conf

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=./hitler.sh gunicorn
directory=/home/abhirath/Desktop/Hitler

[program:celery]
command=./hitler.sh celery
directory=/home/abhirath/Desktop/Hitler


stderr_logfile, autostart, autorestart, stopasgroup, killasgroup same as #1




I also tried using command=bash -c "command here" even though I feel it isn't required in the all the above cases. It's mentioned here in the documentation.

I get the following errors and I'm not able to figure out why:-


Could not spawn

Process Exited too quickly


Help is greatly appreciated.

Answer

In the case of #2, you don't actually need to activate the virtualenv. You can change it to this:

[group:hitler]
programs=gunicorn,celery

[program:gunicorn]
command=/path/to/env/bin/gunicorn gunicornserve:app -b 0.0.0.0:3333 -w 2
directory=/home/abhirath/Desktop/Hitler

[program:celery]
command=/path/to/env/bin/celery -A testrunner worker --concurrency=4 --loglevel=info
directory=/home/abhirath/Desktop/Hitler