简体   繁体   中英

How do i write a shell script for checking website live or not

I am writing a shell script to monitor website live or not and send an email alert below is my code

#!/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;

if i run this code i got

./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 ]] '

Please correct me...

Change it do this:

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) ]

To check the status of a command inside an if , you have to do it like

if [ $(command) ]

while you were using

if [ command]

note also the need of spaces around [ ] : if [_space_ command _space_ ]

Update

Based on Ansgar Wiechers's comment , you can also use the following:

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;

That is,

if command && command

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM