19๐
the one liner below, gets the job perfectly done:
kill -HUP `ps -C gunicorn fch -o pid | head -n 1`
Explanation
ps -C gunicorn
only lists the processes with gunicorn
command, i.e., workers and master process. Workers are children of master as can be seen using ps -C gunicorn fc -o ppid,pid,cmd
. We only need the pid of the master, therefore h
flag is used to remove the first line which is PID text. Note that, f
flag assures that master
is printed above workers.
The correct procedure is to send HUP
signal only to the master. In this way gunicorn
is gracefully restarted, only the workers, not master, are recreated.
13๐
You can run gunicorn with option โ-pโ, so you can get the pid of the master process from the pid file.
For example:
gunicorn -p app.pid your_app.wsgi.app
You can get the pid of the master by:
cat app.pid
- Django queryset for many-to-many field
- Django: Form field size
- Django Nested Groups: Groups in Groups
- How do I install Mezzanine as a Django app?
4๐
This should also work to restart gunicorn:
ps aux |grep gunicorn |grep yourapp | awk '{ print $2 }' |xargs kill -HUP
- Deploying Django (fastcgi, apache mod_wsgi, uwsgi, gunicorn)
- Django serialize to JSON
- Cache a django view that has URL parameters
- Django REST Framework: define fields in nested object?
1๐
Step 1:
Go to /etc/systemd/system/gunicorn.service and open file
add bellow line
PIDFile=/run/gunicorn/gunicorn.pid
--pid /run/gunicorn/gunicorn.pid
Example:
[Service]
PIDFile=/run/gunicorn/gunicorn.pid
WorkingDirectory=/home/django/django_project
ExecStart=/usr/bin/gunicorn --pid /run/gunicorn/gunicorn.pid --name=django_project.....
User=django
Group=django
Step 2:
Go to /etc/tmpfiles.d/ and create new file gunicorn.conf if not exist
add Bellow line
d /run/gunicorn 0755 django django -
where django = user and group name
Step 3:
Reboot your server or /etc/init.d/gunicorn restart
to restart gunicorn to take effect
your pid file location is /run/gunicorn/gunicorn.pid check now..
- Multiple Django apps, shared authentication
- How to remove Add button in Django admin, for specific Model?
- In django + nginx + wsgi, what is a "mysite.sock"
- Django/Python Circular model reference
1๐
Building on krizexโs answer answer, when your master pid
is stored in a file, you can gracefully reload your app in one command like this
$ cat app.pid |xargs kill -HUP
I would have liked to comment on the answer itself but I donโt have enough reputation to comment yet ๐ข.
- How to create charts with Plotly on Django?
- Django test client does not log in
- How do I use perform_create to set a field automatically in Django Rest Framework?
0๐
Hereโs a slightly improved version if you have multiple gunicorn master processes running on the same server:
ps -C gunicorn --no-headers -o ppid,pid | awk '$1=="1" {print $2}' | while read pid; do sudo kill -s HUP $pid; done