Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

$DAEMON -t -c $NGINX_CONF_FILE

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
respawn

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

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

exec $DAEMON -c $NGINX_CONF_FILE

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
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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
else
  logger -t nginx -p daemon.err < $syntax_check_output
  rm -f $syntax_check_output
  exit 1
fi

(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.)

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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