【问题标题】:initscript CentOS launching chroot nginx problemsinitscript CentOS 启动 chroot nginx 问题
【发布时间】:2015-10-20 20:22:56
【问题描述】:

我在这里找到了这个启动 nginx 的初始化脚本:http://www.rackspace.com/knowledge_center/article/centos-adding-an-nginx-init-script。我试图修改它以在 chroot 环境下工作,但事实证明它比我预期的要麻烦。

这是我所拥有的:

#!/bin/sh
#    
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog="nginx"
chroot="/usr/sbin/chroot /nginx
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $chroot $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killall $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killall $prog -HUP
    retval=$?
    echo
}

force_reload() {
    restart
}

configtest() {
    $nginx -t -c $NGINX_CONF_FILE 
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        if rh_status_q; then
            printf "$prog already running\n"
            exit 0
        fi
        $1
        ;;
    stop)
        if ! rh_status_q; then
            printf "$prog not running\n"
            exit 0
        fi
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        if ! rh_status_q; then
            printf "$prog not running\n"
            exit 7
        fi
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        if ! rh_status_q; then
            printf "$prog not running\n"
            exit 0
        fi
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

目前主要的不工作是状态调用。 [root@localhost ~]# /etc/init.d/nginx 状态 nginx 死了,但是 subsys 被锁定了

此错误表明存在锁定文件但没有 pid 文件。这是真的,但是当我使用原始的 nginx 初始化脚本并在非 chroot 环境中启动它时,没有 pidfile,并且状态调用工作正常。

我添加了一些行来通过在后台启动进程而不是使用守护进程来检索 pid: PID=$chroot $nginx -c $NGINX_CONF_FILE > /dev/null 2>&1 $ echo $! 回显 $PID > $pidfile

但这会抓取 chroot 的 pid,它会在 nginx 启动后结束。 Nginx 有一个主 pid 和一个子 pid。抓住这些并将它们放入 pidfile 可能是前进的方向吗?或者还有什么我应该尝试的吗?

我认为这是因为守护进程启动了一个 chroot 进程,它得到了错误的 pid。

【问题讨论】:

    标签: linux nginx centos chroot


    【解决方案1】:

    好吧,这就像 grep 一样简单

    pidfile='/var/run/nginx.pid'
    
    start(){
        ...
        daemon $chroot $nginx -c $NGINX_CONF_FILE
        PID=`ps -ef | grep $nginx | grep -v grep | awk '{print $2}'`
        echo $PID > $pidfile
        ...
    }
    stop() {
        ...
        [ $retval -eq 0 ] && rm -f $lockfile && rm -f $pidfile
        return $retval
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2019-12-17
      • 2018-04-26
      • 2011-10-13
      • 2023-03-28
      • 2018-06-21
      相关资源
      最近更新 更多