[Fixed]-How do I restart gunicorn hup , i dont know masterpid or location of PID file

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.

๐Ÿ‘คrowman

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
๐Ÿ‘คkrizex

4๐Ÿ‘

This should also work to restart gunicorn:

ps aux |grep gunicorn |grep yourapp | awk '{ print $2 }' |xargs kill -HUP
๐Ÿ‘คaris

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..

๐Ÿ‘คMitGuru

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 ๐Ÿ˜ข.

๐Ÿ‘คBerel Levy

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
๐Ÿ‘คThomas

Leave a comment