Is there any way to detect when nginx has completed a graceful shutdown?
Solution 1:
What about something like this?
while [ -n "$(pgrep nginx)" ]
do
some-stuff
done
So, the pgrep will look for any nginx processes, and the while loop will let it sit there until they are all gone. You can change some-stuff to do something useful, like sleep 1; /etc/init.d/nginx stop
, so that it'll sleep for a second, then attempt to stop nginx using the init.d script. You can also put a counter in there somewhere so that you can bring out the heaving kill signals if it's taking too long.
Solution 2:
Why not use the built in flag for this?
nginx -s quit
read the second block where it has this flag explained...
http://nginx.org/en/docs/control.html
Solution 3:
Based on cjc's answer, here is the bash script I created, in case anyone else comes across the same problem:
#!/bin/bash
set -e
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "This script must be run as root."
exit
fi
if [ -n "$(pgrep nginx)" -a -f /var/run/nginx.pid ]; then
PID=$( cat /var/run/nginx.pid )
# Gracefully shutdown nginx
kill -QUIT $PID
# Wait for nginx to stop
while [ -d "/proc/$PID" -a -f /var/run/nginx.pid ]; do
sleep 1
done
# Restart nginx
/usr/sbin/service nginx start
else
echo "nginx is not running."
exit 1
fi