I have an automated deployment workflow that pushes code out to my production servers and triggers database migrations, static file updates, etc. Problem is, gunicorn doesn't automatically reload code changes without the development option --reload, which they recommend not using in production. Instead the instruction is to send an HUP signal to the masterpid. Problem is, I don't know how to retrieve the masterpid in an automated script, though it is easy enough to do manually.

How can I retrieve the materpid value for the gunicorn systemd process in a bash script?


Add the following to the systemd service file for gunicorn, or add it as an override:

ExecReload=/bin/kill -HUP $MAINPID

You can then reload with systemctl reload gunicorn.

  • 2
    Or better yet, gunicorn@application.... Dec 31 '16 at 18:02
  • 1
    @MichaelHampton Not sure what you mean by that... if it is, in fact, a better solution, please describe it in an answer and I'll select it if it is indeed better.
    – Adam
    Jan 3 '17 at 21:02
  • The answer is too brief. Possible to explain?
    – Dewsworld
    Jun 1 '17 at 15:18

More detailed explanation for editing your gunicorn service file (based on the answer of @jordanm):

Edit your service file /etc/systemd/system/my_task.service or a similar path

Description=My task running runicorn

# [...]
ExecStart=/home/my_user/my_task/.venv/bin/gunicorn --workers 3 --bind unix:my_task.sock -m 007 wsgi:app
ExecReload=/bin/kill -HUP $MAINPID # <------ add this line


Save it, then run the following to reload the config

sudo systemctl enable my_task

Then you can run the following to gracefully restart your gunicorn service (this should be run in your deploy script)

sudo systemctl reload my_task gunicorn restart

If you don't run the systemctl enable my_task command, you might get the following error:

Failed to reload my_task.service: Job type reload is not applicable for unit my_task.service. See system logs and 'systemctl status my_task.service' for details.


I have created an answer for similar question here. It could be an easier solution compared to the accepted answer, specially in cases gunicorn is not installed system-wide.

in case link did not work

the one liner below, gets the job perfectly done:

kill -HUP `ps -C gunicorn fch -o pid | head -n 1`


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

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Dave M
    Mar 25 '20 at 12:01
  • @DaveM updated answer
    – rowman
    Mar 26 '20 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.