Heim  >  Fragen und Antworten  >  Hauptteil

So erhalten Sie Testergebnisse in Nginx mithilfe der Shell

Ich möchte ein automatisches Skript schreiben
Testen Sie zuerst die generierte Konfigurationsdatei

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/test.conf

Wenn der Test erfolgreich ist, überschreiben Sie nginx.conf. Wenn er nicht erfolgreich ist, wird ein Fehler zurückgegeben

Allerdings weiß ich nicht, wie ich die Informationen erhalten kann, nachdem ich die Testanweisungen ausgeführt habe. Die Anweisungen wurden alle erfolgreich ausgeführt

習慣沉默習慣沉默2712 Tage vor672

Antworte allen(1)Ich werde antworten

  • 黄舟

    黄舟2017-05-16 17:17:34

    [root@arbiter nginx]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@arbiter nginx]# echo $?
    0
    
    [root@arbiter nginx]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/mime.types
    nginx: [emerg] "types" directive is not allowed here in /usr/local/nginx/conf/mime.types:2
    nginx: configuration file /usr/local/nginx/conf/mime.types test failed
    [root@arbiter nginx]# echo $?
    1
    

    bash 中 $? 用来获取上一条命令的返回值,0 为正常,其他值为异常。

    如果需要获取命令输出,可以按一下方式操作:

    [root@arbiter nginx]# a="`/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/mime.types 2>&1`"
    [root@arbiter nginx]# echo $a
    nginx: [emerg] "types" directive is not allowed here in /usr/local/nginx/conf/mime.types:2 nginx: configuration file /usr/local/nginx/conf/mime.types test failed
    

    其中2>&1的作用是将标准错误重定向到标准输出

    Antwort
    0
  • StornierenAntwort