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
A Fresh Look at Manjaro KDE Plasma
Linux adventures come in many distributions, and Manjaro is no exception. I decided to download the KDE Plasma version of...
Dipping My Toes into the Arch World with Manjaro Cinnamon
I decided it was time to explore a distribution I had no experience with, so I downloaded Manjaro Cinnamon. I...
A look at Fedora 43 Server Edition
I decided to look more closely at the Fedora 43 Server Edition after struggling to set up a BtrFS system...
Reasons for Moving to Linux
Cyndi Bulka, my yoga teacher and co-author of our book, “Linux for Small Business Owners”, and I both switched to Linux for some similar as well as a few different reasons. We did it for our businesses as well as our personal computing needs.
Triangle Linux User Group (TriLUG) February Meeting
Triangle Linux Users Group Topic: Why I use Linux -- And have never used WindowsPresenter: David BothWhen: Thursday, 12 February...
Update on my Oldest Computer
Something interesting happened today that I decided to write about. Not that I don't find interesting things to write about...