Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="[email protected]"

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...

like image 689
Akki Avatar asked Mar 24 '23 06:03

Akki


1 Answers

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
like image 146
fedorqui 'SO stop harming' Avatar answered Apr 24 '23 22:04

fedorqui 'SO stop harming'