Question

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.

Était-ce utile?

La 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`")

Autres conseils

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

pkill gunicorn

or

pkill -P1 gunicorn

should kill all running gunicorn processes

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.

Here is the command which worked for me :

pkill -f gunicorn

It will kill any process with the name gunicorn

To start the service which is running on gunicorn

sudo systemctl enable myproject

sudo systemctl start myproject

or

sudo systemctl restart myproject

But to stop the service running on gunicorn

sudo systemctl stop myproject

to know more about python application hosting using gunicorn please refer here

Start:

gunicorn --pid PID_FILE APP:app

Stop:

kill $(cat PID_FILE)

The --pid flag of gunicorn requires a single parameter: a file where the process id will be stored. This file is also automatically deleted when the service is stopped.

I have used PID_FILE for simplicity but you should use something like /tmp/MY_APP_PID as file name.

If the PID file exists it means the service is running. If it is not there, the service is not running. To stop the service just kill it as mentioned.

You could also want to include the --daemon flag in order to detach the process from the current shell.

kill -9 `ps -eo pid,command | grep 'gunicorn.*${moduleName:appName}' | grep -v grep | sort | head -1 | awk '{print $1}'`

ps -eo pid,command will only fetch process id, command and args out

grep -v grep to get rid of output like 'grep --color=auto xxx'

sort | head -1 to do ascending sort and get first line

awk '{print $1}' to get pid back

One more thing you may need to pay attention to: Where gunicorn is installed and which one you're using?

Ubuntu 16 has gunicorn installed by default, the executable is gunicorn3 and located on /usr/bin/gunicorn3, and if you installed it by pip, it's located on /usr/local/bin/gunicorn. You would need to use which gunicorn and gunicorn -v to find out.

If you have ran it by mistake and you want to stop it without writing an extra script and without restarting your server. So here is a trick.

First, uninstall gunicorn

sudo pip3 uninstall gunicorn # I am using python3 in ubuntu server so I am using sudo and pip3

Then kill the process running for your app's port number (I am assuming port number as 8000 by default).

To get the process number in which your app is running use this:

sudo lsof -i:8000

Result

COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
gunicorn 17XX95 root    6u  IPv4 155XXXX      0t0  TCP localhost:8000 
gunicorn 17XX97 root    6u  IPv4 155XXXX      0t0  TCP localhost:8000 

Then just kill the processes, it is possible that you can get more than one process ids(PID), see the result above.

sudo kill 17XX95 17XX97 

Now if you want to reinstall gunicorn you can install it

sudo pip3 install gunicorn
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top