2016-10-14 2 views
1

nginx의 가상 호스트에서 일부 작업을 수행하는 bash 스크립트가 있습니다. nginx 올바르게 다시로드하지 않으면 오류를 catch하는 방법을 알고 싶습니다 (nginx reload).nginx reload에서 오류 잡기

나는 nginx -t을 사용하는 것으로 생각하지만 bash 스크립트에서 오류를 가로 채지는 못한다.

답변

1

그가 다시로드 할 때 0을 반환 -t의 nginx, 그리고

hbranciforte# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
nginx: configuration file /etc/nginx/nginx.conf test is successful 
hbranciforte# echo $? 
0 
hbranciforte# vim /etc/nginx/nginx.conf 

zsh: suspended vim /etc/nginx/nginx.conf 
hbranciforte# nginx -t     
nginx: [emerg] unknown directive "siiendfile" in /etc/nginx/nginx.conf:16 
nginx: configuration file /etc/nginx/nginx.conf test failed 
hbranciforte# echo $?     
1 

그래서이

#!/bin/bash 

output=$(nginx -t) 

reload_code=$? 

if [ $reload_code -eq 0 ]; then 
    echo "conf is correct" 
else 
    echo "conf is not correct" 
fi 

같은 일을하지만, -t 단지 구성을 확인 것이라는 점을 기억할 수있을 때 뭔가 다른 , 구성을 다시로드하려는 경우 보내야합니다.

nginx -s reload 

    #!/bin/bash 

output=$(nginx -s reload) 

reload_code=$? 

if [ $reload_code -eq 0 ]; then 
    echo "reloaded" 
else 
    echo "could not reload, exit with $reload_code" 
fi