Search code examples
nginxdebianstartupscript

Debian - NGINX startup job (initd) doesn't stop


I just compiled the last Nginx version (1.7) on my debian 7.4 machine based on different web posts and docs merged here. It works fine. I also need to change the startup job to target the new Nginx executable. The result is that I can start but can't stop the http sever with the service command.

Nginx 1.7.0 executable location :

/opt/nginx/sbin/nginx.

I removed the previous initd run nlevels configuration by type in

sudo update-rc.d nginx remove

in /etc/init.d/nginx I replaced these two lines :

# PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# DAEMON=/usr/sbin/nginx

with

PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx

Then set run levels

sudo update-rc.d nginx defaults

The sudo service nginx start works fine, but the stop command doesn't work. sudo service nginx stop does nothing, nginx jobs are stil there

root      3252     1  0 09:17 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
www-data  3253  3252  0 09:17 ?        00:00:00 nginx: worker process

Note : I didn't uninstall the old 1.2 Nginx installation.

It's really boring and error prone to kill and restart the process after each configuration update ....

Thanks for your help.


Solution

  • Fix

    The system needs to know the process ID of the nginx daemon to be able to kill it when the stop request is issued. The file containing this PID must be declared in the PIDFILE variable in the /etc/init.d/nginx startup script AND in the nginx configuration file. I forgot this latter.

    /etc/init.d/nginx

    PIDFILE=/var/run/nginx.pid
    

    /path/to/nginx.conf

    pid     /var/run/nginx.pid;
    

    Here is the whole startup script source

    #! /bin/sh
    ### BEGIN INIT INFO
    # Provides:          nginx
    # Required-Start:    $local_fs $remote_fs $network $syslog
    # Required-Stop:     $local_fs $remote_fs $network $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # X-Interactive:     true
    # Short-Description: nginx-1.7.0
    # Description:       HTTP server and Reverse proxy
    ### END INIT INFO
    
    # Author: Emmanuel Brunet <emmanuel.brunet@live.fr>
    #
    # Please remove the "Author" lines above and replace them
    # with your own name if you copy and modify this script.
    
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="nginx server"
    NAME=nginx
    DAEMON=/usr/sbin/$NAME
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/$NAME
    
    [ -x "$DAEMON" ] || exit 0
    [ -r /etc/default/$NAME ] && . /etc/default/$NAME
    . /lib/init/vars.sh
    . /lib/lsb/init-functions
    
    do_start()
    {
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started
    
        start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
            || return 1
        start-stop-daemon --start  --pidfile $PIDFILE --exec $DAEMON  \
            || return 2
    }
    
    do_stop()
    {
    
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
        [ "$?" = 2 ] && return 2
        rm -f $PIDFILE
        return "$RETVAL"
    }
    
    do_reload() {
        #
        # If the daemon can reload its configuration without
        # restarting (for example, when it is sent a SIGHUP),
        # then implement that here.
        #
        echo "reloading" $DESC
        start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
        return 0
    }
    
    case "$1" in
      start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
      stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
      status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
      #reload|force-reload)
        #
        # Insert code here if needed
        #;;
      restart|force-reload)
        #
        # Insert code here if needed
        #
        echo "Restarting $DESC"
        do_stop
        case "$?" in
          0|1)
            do_start
            case "$?" in
                0) log_end_msg 0 ;;
                1) log_end_msg 1 ;; # Old process is still running
                *) log_end_msg 1 ;; # Failed to start
            esac
            ;;
            *)
            # Failed to stop
            log_end_msg 1
            ;;
        esac
        ;;
            *)
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
        esac
    

    Hope that helps.