linuxshelltimeoutpingcentos5

shell script - loop stops when ping fails


I'm trying to write a shell script to ping every server listed in a specified file and get its uptime. The intended behavior is that if a server is not reached after ${TIMEOUT} seconds, the script stops trying to ping that server and moves on to the next one in the list. However, when a ping for a server times out, the program stops completely instead of just moving on to the next server in the list. How can I fix this?

Here is the relevant code snippet (${ALS_HOSTS} is the list of servers to be pinged):

while read hostid
do
        ping -c ${NUM_PING_PACKETS} ${hostid}
        if ping -w ${TIMEOUT} -c ${NUM_PING_PACKETS} ${hostid} | grep ' from' 2>/dev/null 1>&2 # check for output
        then
                ssh ${hostid} uptime > ${WORKDIR}/${hostid} &
        fi
done  < ${ALS_HOSTS}

(this script will be running on a CentOS 5 machine that cannot be upgraded or reconfigured at this time)


Solution

  • This could be because ssh shares stdin with the while read loop, and drains all the data to send it to the remote host.

    Use ssh -n to prevent this:

    ssh -n ${hostid} uptime > ${WORKDIR}/${hostid} &