Home > Net >  Shell script for monitoring the sever
Shell script for monitoring the sever

Time:02-16

Can someone please give shell script for this scenario. I need to check below Server/URL is up or not. If my server is up no need to trigger any alert .. if my server is down .. Alert should be triggered by stating my server is down.

http://18.216.40.147:5555/

#!/bin/bash
addr=18.216.40.147
if ! curl -v -u 'Administrator:******' https://"$addr":5555 | grep HTTP/1.1 200; then 
    echo "Server is down" | mailx -s "server is down" "******@gmail.com"
    echo "Server is down" >&2
fi

CodePudding user response:

curl can return only http response code, then you can compare with right answer:

#!/bin/bash

addr="http://18.216.40.147:5555/"
answer=$(curl -o /dev/null -s -w '%{http_code}\n' $addr)

if [ $answer -eq 200 ]
then
  echo "UP"
else
  echo "DOWN"
fi

CodePudding user response:

Suggesting @Juarnir Santos is essentially correct but need some improvements:

  1. If server is down or network configuration is blocking access to server, you want to timeout the request for 4 (or more) seconds.

  2. Bash testing for numbers requires [[ numerical expression ]]

Therefore the improved answer is:

#!/bin/bash

addr="http://18.216.40.147:5555/"
answer=$(timeout 4 curl -o /dev/null -s -w '%{http_code}\n' $addr)

if [[ $answer -eq 200 ]]; then
  echo "UP"
else
  echo "DOWN"
fi
  • Related