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
Why should I subscribe to Both.org?
We're hoping you'd want to subscribe to our site to begin conversations with our writers and with other readers. This...
The Linux Philosophy for SysAdmins, Tenet 15 — Strive For Elegance
Author’s note: This article is excerpted in part from chapter 17 of my book, The Linux Philosophy for SysAdmins, with...
A quick look at DNF5
DNF5 is the new DNF. With Fedora 41, it replaces DNF, and brings with it a new command as well...
Creating photo collages with Linux
Open source software is amazing and it seems like there are always solutions that come in handy when I look...
How I used fdisk in a script to partition a drive
Image by Gerd Altmann on Pixabay. As you probably know by now, I believe in automating everything. So when one...
21st Century System Administration (Or: How I learned to stop worrying and love systemd)
Let me introduce myself. I saw an article here encouraging others to write their first (and maybe subsequent?) articles about...