
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 caution about using sudo
This example shows why it is important that users should not be able to edit scripts that they can run with sudo.
Rethinking su vs sudo
If you've been hanging around Both.org for a while, you've undoubtedly noticed that I much prefer using the su -...
ATO book signing schedule announced
The ATO organizers have announced the book signing schedule for All Things Open 2025. I'll be signing copies of three...
Email can’t access the INBOX
If you've been reading my posts here, or my books, you know that I like to experiment with many different...
Choose your new computer’s operating system
I recently built a new computer, and now it needs a fresh operating system. But before I install it, I...
Planning for End of Life
We hear a lot about end of life (EOL) planning for hardware and software. At this time, the end of...