How to get test results in nginx using shell
習慣沉默
習慣沉默 2017-05-16 17:15:34
0
1
633

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

習慣沉默
習慣沉默

reply all(1)
黄舟
[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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!