
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
Read long lines with getline
Getline offers a more flexible way to read user data into your program without breaking the system.
Rhythmbox is a great music player for GNOME
I like Rhythmbox as my music player on Linux because it’s simple and stays out of my way.
How do I choose a Linux distribution?
I see a lot of potential Windows refugees asking this question on the two social media sites I still use,...
Using QGIS and MerginMaps for Geospatial Data Work
Recently, a friend asked me if I could help him map his walk around the area where he lives. I...
Exploring GNU Algol 68: Formatting numbers as strings for output
As I mentioned in my introductory article to this mini-series, GNU Algol 68 is in development, and as of the...
What you need to know when you buy a new computer
Author's Note: This article is based on parts of Chapter 4, "Using Linux – Choosing Hardware," of my book, "Linux...