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
System76 and the New COSMIC Desktop
System76 is celebrating 20 years, and they did so by officially releasing Pop!_OS 24.04 LTS and the COSMIC desktop. I got an...
Everything you never wanted to know about the /etc/fstab File
Mounting of filesystems during the startup process is managed by the /etc/fstab configuration file.
Linux and AI: Why Efficiency Still Matters
Choosing the Right Hardware My daily driver is a System76 Meerkat, powered by an Intel Core i7-255H and with 32...
What do AI and Timothy Leary have in common?
If you were around in the '60s, you probably know who Timothy Leary was. If you weren't around then, the...
A New Way to Watch Your Linux System Work
Here are 3 ways you can keep an eye on your Linux system performance.
The secret to fixing a mangled filesystem
Modern computers and the storage devices we use in them, both HDDs and SSDs, are quite resilient. It's seldom that...