Background:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04 LTS"
I've built nginx, and I'd like to use upstart to start it:
nginx upstart script from the site:
description "nginx http daemon"
start on runlevel 2
stop on runlevel 0
stop on runlevel 1
stop on runlevel 6
console owner
exec /usr/sbin/nginx -c /etc/nginx/nginx.conf -g "daemon off;"
respawn
I get "unknown job" when i try to use initctl to run it, which I just learned apparently means there is an error, ( what's wrong with "Error" to describe errors?)
Can someone point me in the right direction ? I've read the documentation , as it is, and it seems kind of sparse for a SysV init replacement... but whatever just need to add this job to the list, run it, and get on with what's left of my life... Any tips?
EDIT: initctl version init (upstart 0.6.5)
I've ended up here more than once so I thought I'd provide an updated answer based on my own experience after using the answers here. Thanks especially to @danorton and @orj for their answers.
This script has been tested on Upstart 1.5 running on Ubuntu 12.04 with Nginx 1.0.11 and Passenger 3.0.11. If you're not using Passenger you may need to play around with the
post-stop
line. Refer to the Upstart cookbook.In an empty
/etc/init/nginx.conf
add the following lines (You can remove the comments if you like):I've taken the Upstart script from the Nginx Wiki and tweaked it as a number of lines are not needed, cause confusion or do not work.
You may need to alter
env DAEMON
andenv PID
lines depending on where you have installed nginx and are writing the PID. The PID can be configured in nginx.I tried all forms of
expect
. Onlyexpect fork
seems to work. With Passenger nginx creates 61 forks. Upstart requires 0, 1 or 2. As others have hinted, Upstart will be tracking the wrong PID. I've also removedrespawn
as it does nothing probably because of the same reason. Some additional pre/post-start script may be able to fix that by grabbing the real PID. I, however, use monit to handle restarts so do not need it.Do not use
daemon off
. This is for development only. See http://wiki.nginx.org/CoreModule#daemonReferences:
You cannot have multiple
stop on
directives in an upstart job description for Upstart >= 0.5.And
console owner
is probably not what you want (this makes nginx the owner of the system console).Try:
You can’t. At least not properly, anyway.
Nginx does not spawn its daemon in one of the two ways that upstart requires, either via “expect fork” or “expect daemon”, so upstart is unable to track the master nginx process. There are some hacks, but they have their own problems.
If you’re okay with the fact that upstart can’t keep track of the master process and kill it on shutdown, this will work:
There is an Upstart config file example in the NGINX Wiki.
You may need to adjust the path to the nginx binary in the config file.
This config file is working fine for me with Ubuntu 10.04 and nginx 1.0.5.
I also installed an
nginx
symlink in/etc/init.d
pointing at/lib/init/upstart-job
so I could use the standardservice
command to start and stopnginx
.Note: If you install Phusion Passenger with NGINX you might need to add the following stanza to the Upstart config script:
I found this necessary on my Ubuntu config. Otherwise when I issued
initctl stop nginx
orservice nginx stop
nginx didn't actually stop. I also noticed that Upstart thought the nginx process had a PID that was actually the PID of one of the Passenger processes. So clearly NGINX/Passenger is confusing Upstart a little.I use:
The stop on
runlevel [!...]
seems to be more standard. It's what the stock ssh/samba scripts do. You should also add therespawn
bit so it restarts if it dies. I'm also not sure why you wantconsole output
that simply sends console output to stdout. The default behavior is to simply send console output to the logger.You can see all the stanza docs on the Upstart wiki
See http://geeknme.wordpress.com/2009/10/15/getting-started-with-upstart-in-ubuntu for more.
Oddly none of the answers here actually works fully as they leave upstart in a stop/killed state which prevents another start working. This means that
restart nginx
fails.The bug with upstart is well documented at https://bugs.launchpad.net/upstart/+bug/406397 and I'm astonished that the author of upstart doesn't seem to care enough to fix it. The only solution I've seen that works is the following (stolen from the same bug report):
The advantage of writing it like this is that even the respawn works. The disadvantage is that it's ugly and a nasty hack.