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:You can then reload with
systemctl reload gunicorn
.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 pathSave it, then run the following to reload the config
Then you can run the following to gracefully restart your gunicorn service (this should be run in your deploy script)
If you don't run the
systemctl enable my_task
command, you might get the following error: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:
Explanation
pc -C gunicorn
only lists the processes withgunicorn
command, i.e., workers and master process. Workers are children of master as can be seen usingps -C gunicorn fc -o ppid,pid,cmd
. We only need the pid of the master, thereforeh
flag is used to remove the first line which is PID text. Note that,f
flag assures thatmaster
is printed above workers.The correct procedure is to send
HUP
signal only to the master. In this waygunicorn
is gracefully restarted, only the workers, not master, are recreated.