How I delay code execution in Bash until a remote host is responding
I have recently been working on some code that needs to run on a remote host using SSH. I describe this in my article, Using tar and ssh for backups.
But now I need to reboot the remote computer and wait until it has completely rebooted and the network is up and running. Only after that occurs can I continue sending commands to that remote system.
This little bit of code accomplishes that for me.
Host="f41vm"
X=1
until [ $X -eq 0 ] ; do
sleep 10s
ping -c1 $Host
X=$?
done
# The rest of the code is executed starting here,
# when the ping is successful and
# returns 0 instead of 1.
The X=$? statement takes the return code of the ping command ( $?_ ) and sets the $X variable to that value. Then $X can be used in the until statement comparison expression.
I add the sleep command so I’m not sending a Bazzillion pings to the remote host, and 10 seconds seems to work well enough for most of my purposes.
More Stories
Announcing the 3rd Edition of “Using and Administering Linux”
I've just signed the contracts for the 3rd Edition of my "Using and Administering Linux" series of books, that together...
Fresh KDE Innovation Meets Ubuntu Stability
Recently, I came across a blog post about the latest release of Plasma KDE Neon, and it piqued my curiosity. I...
Getting Help for Linux
If you're a newcomer to Linux, or you are considering making the move to Linux, one question you'll most certainly...
Nine traits of the veteran Unix admin
Follow this field guide if you want to understand the rare and elusive hard-core Unix geek
A Distributed WordPress Cracking Attack
If you're running a server connected to the Internet, it's under constant attack from the crackers wanting to steal personal...