Jan 07 2009

Shell script for checking a sytem alive or not

Category: Shell ScriptsBipin Balakrishnan @ 6:08 pm

It is necessary in certain cases we need to check whether the system  or server is alive and if we want some alert like email or sms to inform you that the system is down.We can do it by a simple shell script .All we need is to install a package call fping

fping  is a program like ping which uses the Internet Control Message Protocol (ICMP) echo request to determine if a target host is responding.  fping differs from ping in that you can specify any number of targets on the command  line,  or  specify a file containing the lists of targets to ping. Instead of sending to one target until it  times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion.
installing command

sudo apt-get install fping

Script:

#!/bin/sh
status=`fping 192.168.1.22 | cut -d ' ' -f 3`
if [ $status != "alive" ]
then
echo 'system is down' //Here use your alert as you like email, sms, voice alert etc
fi

Will check the system with IP 192.168.1.22 alive or not.Put it as a cron job and schedule the time interval to automatically check system status and get the alert…:)

Tags: ,

4 Responses to “Shell script for checking a sytem alive or not”

  1. Binny V A says:

    In the given script, there is just one ip address – so you could use ping for this, right?

  2. admin says:

    Yes Binny its pinging for only one system.I have written only the basic logic.You can modify according to your requirement.

  3. Bart says:

    This would be much more efficient and scriptable and accepts the IP to be checked as argument.

    #!/bin/sh
    fping -q $1 > /dev/null 2>&1
    if [ $status != 0 ]
    then
    echo ’system is down’ //Here use your alert as you like email, sms, voice alert etc
    fi

  4. Bipin Balakrishnan says:

    @ Bart: The script not used for manual run its used here as a corn so no need of argument here.

Leave a Reply