Nginx部署,写了一个shell脚本,为什么报这个错误

小刘
  • 526
#!/bin/bash
# chkconfig: - 85 15
# description: Nginx server control script
# processname: nginx
# config file: /usr/local/nginx/conf/nginx.conf
# pid file: /usr/local/nginx/logs/nginx.pid
# 
# eastmoney public tools
# version: v1.0.0
# create by Abbott Liu, 2021-9-15
# 

# source function library
. /etc/rc.d/init.d/functions

NGINX_NAME="nginx"
NGINX_PROG="/usr/local/sbin/nginx"
NGINX_PID_FILE="/usr/local/nginx/logs/nginx.pid"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
NGINX_LOCK_FILE="/var/lock/subsys/nginx.lock"

# check current user
[ "$USER" != "root" ] && exit 1

start() {
    status
        if [[ $? -eq 0 ]]; then
            echo $"Nginx (PID $(cat $NGINX_PID_FILE)) already started."
            return 1
        fi
    echo -n $"Starting $NGINX_NAME: "
        daemon $NGINX_PROG -c $NGINX_CONF_FILE
        retval=$?
        echo
    [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE
    return $retval
}

stop() {
    status
        if [[ $? -eq 1 ]]; then
            echo "Nginx server already stopped."
            return 1
        fi
    echo -n $"Stoping $NGINX_NAME: "
        killproc $NGINX_PROG
        retval=$?
        echo
    [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE
    return $retval
}

restart() {
    stop
        sleep 1
    start
    retval=$?
    return $retval
}

reload() {
    echo -n $"Reloading $NGINX_NAME: "
        killproc $NGINX_PROG -HUP
        retval=$?
        echo
    return $retval
}

status() {
    netstat -anpt | grep "/nginx" | awk '{print $6}' &> /dev/null
        if [[ $? -eq 0 ]]; then
            if [[ -f $NGINX_LOCK_FILE ]]; then
                return 0
            else
                return 1
            fi
        fi
    return 1
}

_status() {
    status
        if [[ $? -eq 0 ]]; then
            state=`netstat -anpt | grep "/nginx" | awk '{ print $6 }'`
            echo $"Nginx server status is: $state"
        else
            echo "Nginx server is not running"
        fi
}

test() {
    $NGINX_PROG -t -c $NGINX_CONF_FILE
        retval=$?
    return $retval
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    reload)
        reload
        ;;
    restart)
        restart
        ;;
    status)
        _status
        ;;
    test)
        test
        ;;
    *)
        echo "Usage: { start | stop | reload | restart | status | test }"
        exit 1
esac

回复
阅读 302
1 个回答

问题应该是出在这一行 daemon $NGINX_PROG -c $NGINX_CONF_FILE
把这行单独拿出来运行看看是不是报错了

我建议是这种场景应该直接换成systemd,写一个service文件很方便就能处理好。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏