奇怪的是这对我来说是行不通的。
nginx -t | grep -c 'successful' | awk '{if ($0==0) print "not found"; else if ($0>0) print "found"}'它返回一个发现的的not,尽管结果如下:
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命令是否成功吗?
发布于 2018-10-06 16:30:32
通过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相比,输出更好,因为消息可能在将来被本地化或发生其他更改。
https://serverfault.com/questions/934289
复制相似问题