奇怪的是,这对我来说不起作用。
nginx -t | grep -c 'successful' | awk '{if ($0==0) print "not found"; else if ($0>0) print "found"}'
它返回一个未找到即使结果如下:
nginx: [warn] conflicting server name "xxx.co.za" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "xxx.co.za" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
not found
即使是简单的 grep 也无法达到预期的效果。
(nginx -t | grep -q 'successful') && echo "Yes" || echo "Nope"
知道如何检查nginx -t命令是否成功?
答案1
通过 nginx 命令的退出值,如果成功则为 0。
root@nginx-1-vm:~# nginx -t && printf "Valid\n" || printf "Error\n"
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Valid
root@nginx-1-vm:~# mv /etc/nginx/nginx.conf /tmp
root@nginx-1-vm:~# nginx -t && printf "Valid\n" || printf "Error\n"
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
Error
比 grep 输出更好,因为消息将来可能会被本地化或以其他方式更改。