
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
Tips for faster note taking with Joplin
Joplin has become my default note-taking app. I use it for everything, whether short notes on what to pick up...
Ten moments that shaped Linux
This article was originally posted on opensource.com. It's been updated and is published here with the author's permission. In August...
My six tenets for learning to be a Linux SysAdmin
I have been working with Linux operating systems for a little over a year and a half now, and as...
Three surprising reasons I like the Cinnamon desktop
I wrote about my five reasons to like Cinnamon back in April, and in the time since, I've found even...
My Journey From Zero to Linux SysAdmin
I never expected that I would be where I am today in my Linux journey especially because I never expected...
Making FORTRAN 77 easier to read
If you’re getting started with FORTRAN, add this pretty printer to your programming toolkit.