I busted my brains trying to figure out where your script calls itself. Finally, decided to run it with one modification. The variable $?
returns the exit status of the last command, or according to BASH's textinfo pages:
? Expands to the exit status of the most recently executed fore‐
As it is in your script it will return the exit status of the read NODE
command. Moving it to after the ping
command and running your script did not create (according to top) any additional processes.
You can also do away with the if-then statement by using the &&
commands. Also redirect both stderr and stdout to /dev/null.
Here my modified script:
while true; do
while read NODE ; do
ping -c 1 -w 1 $NODE &>/dev/null && echo "$NODE alive" || echo "$NODE failed"
done < tunnel_ips.txt >> pingscript-output.txt
Or, if you must use if-then:
if ping -c 1 -w 1 $NODE &>/dev/null ; then
echo "$NODE alive"
echo "$NODE failed"
I would also recommend a timestamp:
echo "$(date +%H":"%M) $NODE alive"