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
First Look at Fedora 43
Fedora 43 dropped yesterday, October 28, right on schedule. It's nice to know that I can count on version upgrades...
Fedora 43 Released
Fedora 43 has just been released. The best place to find the link to download Fedora 43 is on the...
Fedora 43 set for release October 28, 2025
Based on the current schedule for Fedora development, Fedora 43 should be released tomorrow, October 28. Although a delay is...
Setting up OBS Studio to record videos
Here’s how I configure and use OBS Studio so I can create videos for my YouTube channel.
Free Office Software That Works
Recently, I had a client who, despite my recommendation for Linux or macOS, chose instead to opt for Microsoft Windows...