'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:[1]
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 2:[2]
To see the processes is ps ax|grep gunicorn
and to stop gunicorn_django is pkill gunicorn
.
Solution 3:[3]
pkill gunicorn
or
pkill -P1 gunicorn
should kill all running gunicorn processes
Solution 4:[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:[5]
Here is the command which worked for me :
pkill -f gunicorn
It will kill any process with the name gunicorn
Solution 6:[6]
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
Solution 7:[7]
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.
Solution 8:[8]
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.
Solution 9:[9]
The above solutions does not remove pid file when the process is killed.
cat <pid-file> | xargs kill -2
This solution reads pid file and send interrupt signal. This closes gunicorn properly and pid file is also removed.
PID file can be generated by
guncorn --pid PID-FILE
or by adding the following in config file
pidfile = "pid_file"
Solution 10:[10]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow