如何编写用于检查网站实时的shell脚本

时间:2013-07-08 08:46:07

标签: bash shell if-statement

我正在编写一个shell脚本来监控网站是否存在,并在下面发送电子邮件提醒是我的代码

#!/bin/bash
if [[ curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]]
then echo "The HTTP server on opx.com and oss.com is up!" #> /dev/null
else
msg="The HTTP server  opx.com Or oss.com is down "
email="opx-noc@opx.com"

curl --data "body=$msg &to=$email &subject=$msg" https://opx.com/email/send
fi;

如果我运行此代码我

./Monitoring_Opx_Oss: line 2: conditional binary operator expected
./Monitoring_Opx_Oss: line 2: syntax error near `-s'
./Monitoring_Opx_Oss: line 2: `if [[ curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]] '

请纠正我......

1 个答案:

答案 0 :(得分:6)

更改它:

if [ $(curl -s --head  --request GET http://opx.opera.com/opx/version | grep "200 OK" > /dev/null) ] && [ $(curl -s --head --request GET http://oss.opera.com/version | grep "200 OK" > /dev/null) ]

要检查if内部命令的状态,您必须执行

if [ $(command) ]

当你使用

if [ command]

还要注意[ ]周围的空格:if [_space_ command _space_ ]

更新

基于Ansgar Wiechers's comment,您还可以使用以下内容:

if curl -s --head  --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null && curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null;

即,

if command && command