40๐
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`")
140๐
To see the processes is ps ax|grep gunicorn
and to stop gunicorn_django is pkill gunicorn
.
- [Django]-URL-parameters and logic in Django class-based views (TemplateView)
- [Django]-How can I handle Exceptions raised by dango-social-auth?
- [Django]-How exactly do Django content types work?
- [Django]-How to register users in Django REST framework?
- [Django]-Django Rest Framework remove csrf
- [Django]-How to pass information using an HTTP redirect (in Django)
19๐
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.
- [Django]-What's the difference between `from django.conf import settings` and `import settings` in a Django project
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Loading initial data with Django 1.7+ and data migrations
17๐
Here is the command which worked for me :
pkill -f gunicorn
It will kill any process with the name gunicorn
- [Django]-Get list item dynamically in django templates
- [Django]-Count() vs len() on a Django QuerySet
- [Django]-How does django handle multiple memcached servers?
15๐
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.
- [Django]-Django โ filtering on foreign key properties
- [Django]-Github issues api 401, why? (django)
- [Django]-How to solve "Page not found (404)" error in Django?
5๐
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
- [Django]-H14 error in heroku โ "no web processes running"
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-How to access Enum types in Django templates
3๐
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.
- [Django]-Django Admin app or roll my own?
- [Django]-How do you dynamically hide form fields in Django?
- [Django]-Django.db.migrations.exceptions.InconsistentMigrationHistory
3๐
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
gunicorn --pid PID-FILE
or by adding the following in config file
pidfile = "pid_file"
- [Django]-How can I use the variables from "views.py" in JavasScript, "<script></script>" in a Django template?
- [Django]-How to combine multiple QuerySets in Django?
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
3๐
In your terminal, do:
ps ax|grep gunicorn
Then to kill the Gunicorn process, just do that:
kill -9 <gunicorn pid number>
In my case I dealt with many processes
For example: kill -9 398 399 4225 4772
- [Django]-Unresolved attribute reference 'objects' for class '' in PyCharm
- [Django]-Where does pip install its packages?
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
3๐
If we run:
pkill gunicorn
We stop all gunicorn services, in this case to start gunicorn we only need to stop the parent process associated with the service that attends the port where gunicorn will be executed.
The following script searches for said process (pid), if it exists it kills this process:
#!/bin/bash
# ---------------------
stop_unicorn_on_port() {
pid=$(lsof -w -t -i "TCP:${1}" | head -1)
if [ -z "${pid}" ]; then
echo "๐ฆ no service deamon on port ${1}"
else
kill -9 "${pid}"
echo "๐ฆ killed service deamon(${pid}) on port ${1}"
fi
}
# Example/Testing
stop_unicorn_on_port 5000
stop_unicorn_on_port 5001
stop_unicorn_on_port 5002
more info check: man lsoft
-
-t
specifies that lsof should produce terse output with process identifiers only and no header โ e.g., so
that the output may be piped to kill(1). -t selects the -w option. -
-i
selects the listing of files any of whose Internet address matches the address specified in i. If no
address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network filesโฆ
Here are some sample addresses:
-i6 - IPv6 only
TCP:25 - TCP and port 25
@1.2.3.4 - Internet IPv4 host address 1.2.3.4
- [Django]-How to go from django image field to PIL image and back?
- [Django]-Django filter many-to-many with contains
- [Django]-Django models.py Circular Foreign Key
0๐
I built upon @Davidโs recommendation to use โpid (PID_FILE) to fix the problem I faced because killing the parent pid didnโt kill worker processes.
import os
import sys
import psutil
def stop_pid(pid):
if sys.platform == 'win32':
p = psutil.Process(pid)
p.terminate() # or p.kill()
else:
os.system('kill -9 {0}'.format(pid))
def get_child_pids(ppid):
pid_list = []
for process in psutil.process_iter():
_ppid = process.ppid()
if _ppid == ppid:
_pid = process.pid
pid_list.append(_pid)
return pid_list
def send_kill_cmd(ppid, cpids):
stop_pid(ppid) # Killing the parent proc first
for pid in cpids:
stop_pid(pid)
if __name__ == '__main__':
parent_pid = int(sys.argv[1])
child_pids = get_child_pids(parent_pid)
send_kill_cmd(parent_pid, child_pids)
Then finally excecuted above python script with below commands
#!/bin/bash
FILE_NAME=PID_FILE
if [ -f "$FILE_NAME" ]; then
pypy stop_gunicorn.py "$(cat PID_FILE)"
echo "killed - $(cat PID_FILE) and it's child processes."
sleep 2
fi
echo 'Starting gunicorn'
nohup gunicorn --workers 1 --bind 0.0.0.0:5050 app:app --thread 50 --worker-class eventlet --reload --pid PID_FILE > nohup_outs/nohup_process.out &
- [Django]-How can I change the default Django date template format?
- [Django]-Django development IDE
- [Django]-Get last record in a queryset
- [Django]-Saving ModelForm error(User_Message could not be created because the data didn't validate)
- [Django]-How do I POST with jQuery/Ajax in Django?
- [Django]-Pylint "unresolved import" error in Visual Studio Code
0๐
I find that specifying the pid file location is the way to go.
$PIDFILE = '/var/etc'
gunicorn master.wsgi:application --pid $PIDFILE --bind=127.0.0.1:8090
A pid file will be created to the location upon running. Gunicorn will stop properly if you remove that pid file.
- [Django]-Extend base.html problem
- [Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist
- [Django]-How to completely dump the data for Django-CMS