Skip to content

Instantly share code, notes, and snippets.

@srpouyet
Last active August 5, 2018 22:37
Show Gist options
  • Select an option

  • Save srpouyet/3006274 to your computer and use it in GitHub Desktop.

Select an option

Save srpouyet/3006274 to your computer and use it in GitHub Desktop.
Nginx Upstart script (Ubuntu 12.04)
### Nginx upstart script
### source: http://serverfault.com/a/391737/70451
### /etc/init/nginx.conf
description "nginx http daemon"
start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]
env DAEMON=/usr/local/sbin/nginx
env PIDFILE=/var/run/nginx.pid
# Needed to allow Nginx to start, however, the wrong PID will be tracked
expect fork
# Test the nginx configuration (Upstart will not proceed if this fails)
pre-start exec $DAEMON -t
# Ensure nginx is shutdown gracefully
# Upstart will be tracking the wrong PID so the following is needed to stop nginx
post-stop exec $DAEMON -s stop
# Start Nginx
exec $DAEMON
@brookemckim

Copy link
Copy Markdown

Working for me without the post-stop line. Ubuntu 12.04.1 and nginx 2.1.4

@jevy

jevy commented Feb 4, 2013

Copy link
Copy Markdown

As stated in init file, the incorrect PID will be found. This causes problems if you have lots of forks (like if you use Passenger) Try this one instead: https://gist.github.com/2492523

@srpouyet

Copy link
Copy Markdown
Author

I updated the script; the previous version didn't stop nginx correctly anymore.
The post-stop trigger now runs $ nginx -s stop to stop the current nginx instance.

@jesuisbonbon

Copy link
Copy Markdown

i love you :)

@kmiscia

kmiscia commented Aug 20, 2013

Copy link
Copy Markdown

Seeing the same problem on Ubuntu 12.04.1 w/ nginx 1.2.3....post-stop addition at least gets it working.

@neilACC

neilACC commented Jan 30, 2014

Copy link
Copy Markdown

Working for me without the post-stop line. CentOS 6.x
"pre-stop exec $DAEMON -s stop" is ok

@annejohnson

Copy link
Copy Markdown

This helped me troubleshoot the screwy nginx stops on my server. Ubuntu 12.04.4.
The line that worked for me was:
post-stop exec $DAEMON -s quit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment