Jonathan Briggs
1 min readOct 21, 2016

--

You should be using PIDFile and $MAINPID instead of pkill. What if you were running nginx multiple times for some reason. In several containers, or as a local server for some developer. Who knows what PID would be picked up?

But systemd knows the PID of a service and the $MAINPID variable should get it for you.

Here is the systemd file from Fedora:

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

--

--