V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
holinhot
V2EX  ›  深圳

用 shell 来验证 nginx 配置文件求助

  •  
  •   holinhot · 2016-06-28 18:28:34 +08:00 · 2358 次点击
    这是一个创建于 2851 天前的主题,其中的信息可能已经有所发展或是发生改变。
    nginx 重启前进行一次验证
    #!/bin/bash
    set -x
    nginxbin=`/usr/local/nginx/sbin/nginx -t`
    if [[ ${nginxbin} == *successful* ]]; then
    echo "nginx configure file ok"

    else
    echo "nginx configure file failed"
    fi

    根本不行啊
    7 条回复    2016-07-03 11:38:54 +08:00
    surfire91
        1
    surfire91  
       2016-06-28 19:01:38 +08:00
    因为 sbin 下的 nginx 输出是到标准错误输出
    panzhc
        2
    panzhc  
       2016-06-28 19:52:37 +08:00   ❤️ 1
    if /opt/meida/nginx/sbin/nginx -t >/dev/null 2>&1; then
    echo "nginx configure file ok"
    else
    echo "nginx configure file failed"
    fi
    holinhot
        3
    holinhot  
    OP
       2016-06-29 09:03:50 +08:00
    @panzhc 谢谢,试了下可用。只是不太理解 /opt/meida/nginx/sbin/nginx -t >/dev/null 2>&1; 这句的含义。把错误和正确的都丢到丢弃怎么判断的
    holinhot
        4
    holinhot  
    OP
       2016-06-29 09:34:37 +08:00
    @panzhc
    现在写好了 不太理解。
    if /usr/local/nginx/sbin/nginx -t >/dev/null 2>&1; then
    echo "nginx configure file ok"

    if /etc/init.d/nginx status >/dev/null 2>&1; then
    echo "nginx running"
    /etc/init.d/nginx reload
    else
    echo "nginx is stop"
    /etc/init.d/nginx stop
    /etc/init.d/nginx start
    exit
    fi

    else
    echo "nginx configure file failed"
    exit
    fi
    exuxu
        5
    exuxu  
       2016-06-29 09:44:32 +08:00
    >/dev/null 标准输出到 /dev/null 设备,该设备是『黑洞』
    2>&1 将错误输出( 2 )定向到标准输出( 1 )
    (:节点是不是搞错了?
    holinhot
        6
    holinhot  
    OP
       2016-06-29 12:40:30 +08:00
    @exuxu 上面这个脚本正常工作 我测试过
    panzhc
        7
    panzhc  
       2016-07-03 11:38:54 +08:00
    程序退出到时候有个代码,可以通过 $? 取到,写成下面也一样
    /opt/meida/nginx/sbin/nginx -t >/dev/null 2>&1
    if [ $? -eq 0 ] then
    echo "nginx configure file ok"
    else
    echo "nginx configure file failed"
    fi
    重定向只不过是不想看到多余的输出
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5374 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 08:59 · PVG 16:59 · LAX 01:59 · JFK 04:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.