How can we help you today?

Wait For Ping Response Then Continue Bash

If you are looking to determine the state of a host before actioning something on it then the code snippet below is perfect.

((count = 60))
while [[ $count -ne 0 ]] ; do
    ping -c 127.0.0.1
    rc=$?
    if [[ $rc -eq 0 ]] ; then
        ((count = 1))                   
    else
        sleep 1                          
    fi
    ((count = count - 1))                
done

if [[ $rc -eq 0 ]] ; then           
    echo "The target host is now back online"
    #ANY OTHER ACTIONS IF SUCCESSFUL
else
    echo "The target host failed to respond to ping requests after maximum attempts exceeded."
    #ANY OTHER ACTIONS IF FAILED
fi
Leave a Reply

Your email address will not be published. Required fields are marked *

    ICTU LTD is a company registered England and Wales (Company No. 09344913) 142 Thornes Lane, Wakefield, England, WF2 7RE
    Copyright © 2025 ICTU LTD, All Rights Reserved.