≡ Menu

Simple Linux and UNIX Shell Script Based System Monitoring With ping Command

You can simply monitor your remote system hosted in some remote IDC. There may be many reasons for which system may out of the network. This simple script is useful to monitor your own small network at home or work.

Understanding the ping command

The ping is one of the basic and nifty command to test network connection. For example, you can send 4 ping request to cyberciti.biz with the following command:
$ ping -c 4 cyberciti.biz
$ ping -c 4 router


PING router ( 56(84) bytes of data.
64 bytes from router ( icmp_seq=1 ttl=64 time=1.02 ms
64 bytes from router ( icmp_seq=2 ttl=64 time=0.824 ms
64 bytes from router ( icmp_seq=3 ttl=64 time=0.826 ms
64 bytes from router ( icmp_seq=4 ttl=64 time=0.843 ms
--- router ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3012ms
rtt min/avg/max/mdev = 0.824/0.880/1.027/0.085 ms

Just see output line ‘— router ping statistics —‘ and note down the ‘4 received’ field. This the key to verify that 4 packets send and received successfully. You can extract words ‘4 received’ with the following command:
$ ping -c4 router | grep 'received'

4 packets transmitted, 4 received, 0% packet loss, time 3012ms

Next logical step is to just get number 4 received using the awk command:
$ ping -c4 router | grep 'received' | awk -F',' '{ print $2}'

4 received 

Last step is to just get 4 number and remove received word:
$ ping -c4 router | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'


Shell script to system monitoring with ping command

Now you know how to obtained received packets, it is time to automate entire process with simple script. Here is a sample shell script (download link):

HOSTS="cyberciti.biz theos.in router"


for myHost in $HOSTS
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
    # 100% failed 
    echo "Host : $myHost is down (ping failed) at $(date)"

You can download complete working script which sends an email alert here.

Setup Cron job

In order to run this script every 30 minutes (or as per your requirements), you need to install a script as cron job:
$ chmod +x /path/to/monitorHost
Install the monitorHost script as crontab using the editor:
$ crontab -e
Append the following cronjob entry:
# Monitor remote host every 30 minutes using monitorHost
30 * * * * /home/vivek/bin/monitorHost

Save and close the file.

Share this on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 49 comments… add one }
  • kotnik July 24, 2006, 12:51 am

    This is the script I’m using for the same thing:

    case `ping -qnc 1 google.com 2>&1` in
    *’100% packet loss’*)
    echo “The network is DOWN.”
    exit 1

    One way or around…

  • randomz July 24, 2006, 9:41 am

    Nice, though I think a ( got dropped before *’100% packet loss’*) ?

  • kotnik July 27, 2006, 11:19 am

    No, ( is not missing. Try it:


  • nixCraft July 27, 2006, 7:01 pm


    Short and sweet code :D

  • Max June 9, 2007, 1:43 am

    this is an ingenious script:)
    thanks to share it
    I have just a little question.
    I didn’t find “Email ID”
    can you help me to find it please??

    thanks for your help

  • nixCraft June 10, 2007, 5:29 am

    Go here


  • TonyK June 11, 2007, 11:27 am

    You can skip the awk stuff by using cut like this:
    ping -c4 localhost | grep ‘received’ | cut -f2 -d’,’ |cut -f2 -d’ ‘

    it also returns 4

    • atc0re September 9, 2012, 4:11 pm

      ping -c4 localhost |grep received| cut -d “,” -f2| cut -d ” ” -f2

  • nixCraft June 11, 2007, 3:17 pm


    Yes you can always use another set of commands to do the same thing. Coding is like that… heh

    Appreciate your post!

  • BenV July 17, 2007, 9:46 am

    I commonly use this. Just another way to do it.
    if [[ $(ping -q -c 3 == @(*100% packet loss*) ]]; then
    echo “$host is down”
    echo “$host is alive”

  • John Golini August 11, 2008, 12:26 am

    can someone show me a script I could cron every 15 min or so, that would email ‘root’ each time a computer comes up on the LAN or goes off the LAN, giving the ip address and the windows computer name?

  • Saurabh November 17, 2008, 8:55 am

    Short and sweet code Benv

  • ishfaq February 20, 2010, 7:16 pm

    nice script but any tell how to paste the out put on apache page

  • Amza Marian May 13, 2010, 11:09 am

    @ishfaq :

    echo “Host : $myHost is down (ping failed) at $(date)” >> /var/www/network.txt

  • Ernest June 17, 2010, 5:34 pm

    @BenV – Will your script work if $host does not exist? Typo’s happen.

    Will adding a ‘ping -c1 -w1″ enable a timeout and avoid hanging indefinitely? or is the -w not used that way?

  • difa December 13, 2010, 4:23 am

    thanks you ^,^

  • Bob Watson February 9, 2011, 4:32 pm

    Along a slightly different vein- I’ve got my e-mail client set to start as soon as I log in, which is often before the network is online. It then tells me it can’t find the server and decides to work off-line. This is annoying, so I added a slight modification of the above at the head of its initialisation script:

    while :
    count=$(ping -c 1 imap.1and1.co.uk | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
    if [ $count -eq 1 ]; then
    sleep 5

  • HTF April 3, 2011, 2:24 am

    Quick question:
    If I set the ping count to 4 and only one of four will failed will I get the true positive alarm anyway?

  • Amza Marian April 3, 2011, 2:35 pm

    HTF: Yes

    if [ $count -eq 0 ]; then <- only if 100% failed.

  • Peter April 7, 2011, 12:14 am

    It works fine except i tried to use it to make sure my internet connection was still up and if the internet connection fails It can’t resolve the host so it can’t try ping to ping it. How could i add a check for this situation?

    • nixCraft April 7, 2011, 3:00 am

      Check dns settings in /etc/resolv.conf file including firewall settings.

  • shilpa April 19, 2011, 2:25 pm

    how to check server is alive using ping command at client in unix system programming using ICMP protocol

  • Amza Marian April 19, 2011, 8:18 pm

    @shilpa: PING using icmp protocol. You can read more here:

  • shilpa April 22, 2011, 10:29 am

    have to implement ping command using raw socket in unix

  • shilpa April 22, 2011, 5:58 pm

    i am doing a small program on the working of ping command in linux using icmp protocol. here is the code but its giving an error at getuid()

    char dst_addr[20];
    char src_addr[20];
    unsigned short in_cksum(unsigned short*,int);
    void parse_argvs(char**,char*,char*);
    void usage();
    char *getip();
    char *toip(char*);
    int main(int argc, char *argv[])
      struct iphdr *ip;
      struct iphdr *ip_reply;
      struct icmphdr *icmp;
      struct sockaddr_in connection;
      char *packet=NULL;
      char *buffer=NULL;
      int sockfd;
      int optval;
      int addrlen;
      int siz;
       fprintf(stderr,"%s:rot privileges needed\n",*(argv+0));
      printf("src addr is %s\n",src_addr);
      printf("dest addre is %s\n",dst_addr);
      packet=malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
      buffer=malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
      ip=(struct iphdr *)packet;
      icmp=(struct icmphdr *)(packet + sizeof(struct iphdr));
      ip->tot_len=sizeof(struct iphdr) + sizeof(struct icmphdr);
      ip->check=in_cksum((unsigned short *)ip,sizeof(struct iphdr));
      icmp->checksum=in_cksum((unsigned short *)icmp,sizeof(struct icmphdr));
      sendto(sockfd,packet,ip->tot_len,0,(struct sockaddr *)&connection,sizeof(struct sockaddr));
      printf("sent %d byte packet to %s\n",ip->tot_len,dst_addr);
      if((siz=recvfrom(sockfd,buffer,sizeof(struct iphdr) + sizeof(struct icmphdr),0,(struct sockaddr *)&connection,&addrlen))==-1)
         printf("received %d byte reply from %s\n",siz,dst_addr);
         ip_reply=(struct iphdr *)buffer;
         printf("ID: %d\n",ntohs(ip_reply->id));
       return 0;
       void parse_argvs(char **argv,char *dst,char *src)
         int i;
          if(!(*(argv + 1 ) ) )
         if(*(argv + 1) && (!(*(argv + 2 ) ) ) )
           strncpy(dst,*(argv + 1),15);
         else if((*(argv + 1 ) && (* ( argv + 2 ) ) ) )
           strncpy(dst,*(argv + 1 ),15);
           while(*(argv + i + 1 ) )
             if(strncmp(*(argv + i),"-s",2)==0)
               strncpy(src,*(argv + i + 1),15);
       void usage()
         fprintf(stderr, "\n usage:ping [destination]  \n");
         fprintf(stderr,"destination must be provided\n");
        fprintf(stderr,"source is optional \n \n");
       char * getup()
         char buffer[256];
         struct hostent *h;
         return inet_ntoa(*(struct in_addr *)h->h_addr);
       char * toip(char * address)
         struct hostent *h;
         return inet_ntoa(*(struct in_addr *)h->h_addr);
       unsigned short in_cksum(unsigned short *addr, int len)
         register int sum=0;
         u_short answer=0;
         register u_short *w=addr;
         register int nleft=len;
         while(nleft > 1)
            *(u_char*) (&answer)=*(u_char*)w;
          sum=(sum>>16) + (sum & 0xffff);
  • shilpa April 22, 2011, 5:58 pm

    can anybody suggest me a solution plz

  • Henrik C July 8, 2011, 11:24 am

    Hi coders

    What would i use if i needed 1 timestamp when “Ping up again” ?


  • YC July 20, 2011, 2:19 pm

    This is a great tutorial but there is a little problem. Your cron job won’t be executed every 30 minutes but instead 1 time per hour at 30 minute (1:30, 2:30, 3:30). To run it every 30 min, you should change the first parameter to */30.

  • blognadei October 24, 2011, 10:39 am

    thanks , this work :D

  • ping -qnc 2 google.com > /dev/null 2>&1 && echo ‘Host up!’ || echo ‘Host down!’

  • Der December 4, 2011, 1:06 am

    @Константин Кръстев
    I love your line, but could you explain the bit after the “>” I don’t get that.

    It does work I just don’t know why!


  • Kaizad September 13, 2012, 7:57 am

    Hi friends,can anybody suggest me some code for how to get respond for a client side Ping to a server in C on unix platform?

  • Robby October 7, 2012, 2:57 am

    I like the code but think fping is better at this and easier coding.
    install apt-get install fping
    then put in the code as
    For testing if host alive just use fping -a or fping -u:
    -a show all host in your list as alive or -u shows unavailble.
    You can also predefine using a file with th -f and a filename.

    Long and short you can do what you want by adding:
    a file called something like host.txt and code would be one line such as:
    fping -ag -f /path/host.txt > results
    your host.txt would hold all hosts you want to monitor. This one line will go out and check the hosts listed and let you know everything alive in the results file. if you leave off the -a it will list the status up or down in the results. this is getting wordy and not trying to over ride the post everyone has there own way. I am just giving a quick response of an alternate way of doing it with less lines of code.

    Thanks and great job on the code,
    Robby Loyed

  • giannis December 27, 2012, 6:52 pm

    I think the code could be sligtly improved.

    Sending an email for each machine that is down can lead to a very high number of emails,

    Sending one email reporting just the ones that were down during the check is easier to go through and check.

    # Simple SHELL script for Linux and UNIX system monitoring with
    # ping command
    # -------------------------------------------------------------------------
    # Copyright (c) 2006 nixCraft project 
    # This script is licensed under GNU GPL version 2.0 or above
    # -------------------------------------------------------------------------
    # This script is part of nixCraft shell script collection (NSSC)
    # Visit http://bash.cyberciti.biz/ for more information.
    # -------------------------------------------------------------------------
    # Setup email ID below
    # See URL for more info:
    # http://www.cyberciti.biz/tips/simple-linux-and-unix-system-monitoring-with-ping-command-and-scripts.html
    # -------------------------------------------------------------------------
    # add ip / hostname separated by white space
    HOSTS=" ...."
    # no ping request
    # email report when
    SUBJECT="Ping failed"
    EMAILID="email here"
    for myHost in $HOSTS
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
        # 100% failed
        failedhosts="$failedhosts $myHost"
      if [ $fail = "true" ]; then
            echo $failedhosts
            echo "Hosts down: $failedhosts" | mail -s "$SUBJECT" $EMAILID
    • maya March 19, 2014, 12:35 pm

      I am also using the same procedure to monitor multiple systems

  • Tina March 19, 2013, 12:32 am

    Any suggestions on if I want a script to ping a host every 10 sec 6 times and send an e-mail if packet loss is anything other than 0%?

    • vipin August 12, 2014, 9:29 am

      Simple Script to check whether the remote host is up or down;

      ping -c 6
      if [ “`echo $?`” == 0 ]; then
      echo “ is up”
      echo “ is down”
      sleep 10
      sh /root/script

      Execute it once and it will not stop until you kill the script , put ‘mail’ command in the script and you will receive it in your email if there is no response from the remote host.


  • Hamid April 15, 2013, 4:00 pm

    very good…..
    thank you

  • Mohamed April 17, 2013, 9:50 am

    Really big thanks for your great help

  • Vignesh July 22, 2013, 6:18 am

    I have developed the script such that to ping it forever:

    function CheckHost()
    count=$(ping -s $HOSTS 56 $ping | grep “received” | awk -F, ‘{print $1 $2 $3}’)
    echo $count
    op=$(echo $count | awk ‘{print $1 $4}’)
    echo $op
    if [ $op != $ping$ping ];
    echo “Host : $HOSTS Ping Failed $count” | mailx -s “$SUBJECT” $EMAILID
    echo ‘Calling CheckHost Again!’

  • Aritra September 11, 2013, 5:13 am

    Hi ,

    I was looking for a way by which I can ping the remote UNIX Servers from my Local (WINDOWS) system. Any suggestions on how can this be achieved? I fear that checking the server availability from a unix server may fail if the unix server on which the check script is hosted goes down anyday.


  • sri February 19, 2014, 11:56 am

    Thanks much. It helped me.

  • vipin August 12, 2014, 9:20 am

    Simple Script to check whether the remote host is up or down;

    ping -c 4
    if [ “`echo $?`” == 0 ]; then
    echo “ is up”
    echo “ is down”

    execute it through cron daemon and put use mail command to receive it to your email.


  • KH October 23, 2015, 11:47 pm

    There is a typo in your first awk command (“Next logical step…”) — should use $2, not $4.

    –> $ ping -c4 router | grep 'received' | awk -F',' '{ print $2}'

  • Sayajin January 5, 2016, 2:48 pm

    Why such long ways to do this…

    [ `ping -c 4 domain.com > /dev/null 2<&1; echo $?` -eq 0 ] && echo "Host is up" || echo "Host is Down";

  • Kumaran March 12, 2016, 3:54 pm

    Hi all… I have a script that pings a server emails me if there is no response, but it does not work. Below is the script, can someone please point out what is wrong with it and how can I fix it?

    #!/bin/csh -f
    set dat = `date +'%d-%m-%Y'`
    set  a = `ping -c 2 -w 2 my-host`
    if ($? == 0) then
            echo ""
            mail -s "my-host is Down on `date`" me@mail.com
    • Vivek Gite March 12, 2016, 6:21 pm


      #!/bin/csh -f
      set dat = `date +'%d-%m-%Y'`
      set mydomain = "cyberciti.biz"
      ping -c 2 -q "$mydomain"  >& /dev/null
      if ( $? == 0 ) then
              echo "Up at $dat"
              echo "Down at $dat" 
  • Gino April 1, 2016, 6:48 am

    Hi all,
    Currently i’m run centos crontab unfortunately crontab didn’t perform the job. i’m unsure which part i’m wrong, if i run manually it is working fine without any issue. kindly need you all advise.

    crontab -e
    * * * * * /bin/sh /root/scripts/check.sh

    #!/bin/sh +x
    # no ping request
    for myHost in $HOSTS
      count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
      if [ $count -eq 0 ]; then
        # 100% failed
            echo "$(date) Host : $myHost is down (ping failed)" >> check.log
            echo "$(date) Host : $myHost is up (ping sucessful)" >> check.log
  • solo November 1, 2016, 1:20 pm

    Hi, all ”

    can any one help how to add 3 more host in the script

    Thank you

Leave a Comment

   Tagged with: , , , , , , , ,