How to stop gunicorn properly

I'm starting gunicorn with the Django command python manage.py run_gunicorn. How can I stop gunicorn properly?

Note: I have a semi-automated server deployment with fabric. Thus using something like ps aux | grep gunicorn to kill the process manually by pid is not an option.


Solution 1:

To see the processes is ps ax|grep gunicorn and to stop gunicorn_django is pkill gunicorn.

Solution 2:

One option would be to use Supervisor to manage Gunicorn.

Then again i don't see why you can't kill the process via Fabric. Assuming you let Gunicorn write a pid file you could easily read that file in a Fabric command.

Something like this should work:

run("kill `cat /path/to/your/file/gunicorn.pid`")

Solution 3:

pkill gunicorn

or

pkill -P1 gunicorn

should kill all running gunicorn processes

Solution 4:

pkill gunicorn stops all gunicorn daemons. So if you are running multiple instances of gunicorn with different ports, try this shell script.

#!/bin/bash
Port=5000
pid=`ps ax | grep gunicorn | grep $Port | awk '{split($0,a," "); print a[1]}' | head -n 1`
if [ -z "$pid" ]; then
  echo "no gunicorn deamon on port $Port"
else
  kill $pid
  echo "killed gunicorn deamon on port $Port"
fi

ps ax | grep gunicorn | grep $Port shows the daemons with specific port.

Solution 5:

Here is the command which worked for me :

pkill -f gunicorn

It will kill any process with the name gunicorn