Debian - NGINX启动工作(initd)不会停止

时间:2014-05-03 08:02:12

标签: nginx debian startupscript

我刚刚在我的debian 7.4机器上根据不同的网络帖子和文档合并here编译了最后一个Nginx版本(1.7)。它工作正常。 我还需要更改启动作业以定位新的Nginx可执行文件。结果是我可以启动,但无法使用 service 命令停止http服务器。

Nginx 1.7.0可执行位置:

/opt/nginx/sbin/nginx.

我按

中的类型删除了之前的initd run nlevels配置
sudo update-rc.d nginx remove
/etc/init.d/nginx中的

我替换了这两行:

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

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

然后设置运行级别

sudo update-rc.d nginx defaults

sudo service nginx start 工作正常,但stop命令不起作用。 sudo service nginx stop 什么都不做,nginx的工作就在那里

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

注意:我没有卸载旧的1.2 Nginx安装。

在每次配置更新后,它都非常无聊并且容易出错并重新启动...

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

修复

系统需要知道nginx守护程序的进程ID,以便在发出停止请求时能够将其终止。必须在nginx配置文件中的/etc/init.d/nginx启动脚本 AND 中的PIDFILE变量中声明包含此PID的文件。我忘记了后者。

/etc/init.d/nginx

PIDFILE=/var/run/nginx.pid

/path/to/nginx.conf

pid     /var/run/nginx.pid;

这是整个启动脚本源

#! /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

希望有所帮助。