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
How I create encrypted passwords
I sometimes need to create an encrypted password for use in scripts when adding one or more new user accounts...
How many releases of Fedora can I upgrade from?
Upgrades are an important part of a SysAdmin's job, whether you know you're one or not. If you think about...
Best Articles of 2024 about Linux system tools
Linux has many excellent tools that allow SysAdmins to manage our systems. We've published many articles this year about those...
Happy birthday Both.org
On December 11, 2023. Both.org morphed from being simply my personal website to being a replacement for Opensource.com (OSDC). On...
How to install FreeDOS the old-school way
Here’s how to install FreeDOS the old-fashioned way, with every step done by hand.
Remove Background from Photos for Holiday Card Creation with Python
You can use open-source tools to produce holiday cards this year. Suppose you have a great photo of your family...