Based on the content of the Ubuntu init.d script to control nginx instances, found at : http://wiki.nginx.org/Nginx-init-ubuntu I'm trying to update Upstart version, found at: http://wiki.nginx.org/Upstart to make the configuration syntax check.

Basically what it needs to do is, call:


This should output error messages regarding wrong syntax on configuration files.

I've tried the following without success:

# nginx

description "nginx http daemon"
author "George Shammas <georgyo@gmail.com>"

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env DAEMON=/opt/nginx/sbin/nginx
env PID=/var/run/nginx.pid
env NGINX_CONF_FILE=/etc/nginx/nginx.conf
console output
expect fork

pre-start script
        $DAEMON -t -c $NGINX_CONF_FILE
        if [ $? -ne 0 ]
                then exit $?
end script

post-stop script
    start-stop-daemon --stop --pidfile $PID --name nginx --exec $DAEMON --signal TERM
end script


The problem here is that, it doesn't put the output to console, even with the "console output" stanza.

Am I missing something here?

What I get when it can't start is a simple:

root@localhost:/# start nginx
start: Job failed to start

There's no easy answer here. Unfortunately, if you pipe the output of the syntax check to something like logger, you lose the return code to check if it failed.. so you have to do it something like this:

syntax_check_output=`mktemp /tmp/nginx.check.XXXXXX`
if $DAEMON -t -c $NGINX_CONF_FILE > $syntax_check_output 2>&1 ; then
  rm -f $syntax_check_output
  logger -t nginx -p daemon.err < $syntax_check_output
  rm -f $syntax_check_output
  exit 1

(incidentally, you should note that all upstart script sections are run with 'set -e', so the if statement in your original example won't be executed.. as soon as the command exits non-zero, the shell will exit.)

  • what about some thing like: variable=$($DAEMON -t -c $NGINX_CONF_FILE) the get the output and then, instead of getting it from file: logger -t nginx -p daemon.err $variable. Could it work? – Gabriel Mazetto Jan 19 '12 at 7:43
  • yes that might even be a better idea. :) – SpamapS Jan 22 '12 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.