How do I restart gunicorn hup , i dont know masterpid or location of PID file
Asked Answered
K

6

15

I want to restart a Django server which is running using gunicorn.

I know how to use gunicorn in my system. But now I need to restart a remote server which is not set up by me.

I don't know masterpid to restart the server how can I get the masterPID.

Usually I HUP gunicorn with sudo kill -s HUP masterpid.

I tried with ps aux|grep gunicorn

and I did not find the gunicorn.pid file anywhere.

How can I get the masterpid?

Kristenkristi answered 13/11, 2014 at 6:49 Comment(0)
V
21

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.

Voorhees answered 24/3, 2020 at 15:0 Comment(1)
Awesome. works perfectly.Rummel
C
15

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
Complect answered 21/2, 2017 at 7:52 Comment(0)
H
4

This should also work to restart gunicorn:

ps aux |grep gunicorn |grep yourapp | awk '{ print $2 }' |xargs kill -HUP
Hydrochloride answered 23/1, 2019 at 19:57 Comment(0)
S
3

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

Semiotic answered 9/9, 2022 at 14:16 Comment(0)
U
2

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

Upholster answered 9/3, 2019 at 6:31 Comment(0)
T
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
Thorsten answered 6/6, 2023 at 9:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.