Want to write an automatic script
Test the generated configuration file first
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/test.conf
If the test is successful, overwrite nginx.conf. If it is not successful, an error will be returned.
However, I don’t know how to obtain the information after executing the test instructions. The instructions are all executed successfully
黄舟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
In bash, $?
is used to get the return value of the previous command. 0 is normal, and other values are abnormal.
If you need to get the command output, you can do it as follows:
[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
The function of 2>&1
is to redirect standard error to standard output