Infinite "while" loop subshell loses current date variable

I have a simple script to log network connectivity to a set of systems.

However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue?

#!/bin/bash
LOG=/tmp/netlog
when=`/bin/date`

while true;
do

for server in srv1 srv2 srv3 srv4 srv5;
do

ping -c 1 $server > /dev/null
if [ $? -ne 0 ]; then
 echo "$server-DOWN!" $when >> $LOG.$server

else
        echo "$server-UP!" $when >> $LOG.$server
fi
done

done
Sign In or Register to comment.