FNG Question: Test if remote server has booted

Hello again, I have a script on my media server that wakes up my backup server, performs an 'rsync' backup, then shuts the backup server down. Currently, I have it send the Wake on LAN packet, and sleep for 5 minutes, just to give the backup server time to boot (of course it doesn't take that long, but just in case...). It occurred to me today that that isn't always going to cut it, in the sense that after so many days, the filesystem gets run through fsck (which takes a bit of time on these monster drives).
So, the question... Is it possible to include in a script, maybe a ping mechanism that can be run, then based on the results, determine if the remote server is booted or not? I did some reading on 'ping' (remember, I did use FNG in the title :slight_smile: ) and the man page made a mention of using ping's exit status to determine if a server was up or not, but I honestly am not clear on exactly how I would implement that (i.e. if I used:

if [ ping remotehost ]
then blahblah
else blahblahblah
fi

would that necessarily be looking for the exit code 0 (successful), or am I just way out there? The example above seems to be missing something, but... Thanks again, you guys/girls are a great help!

ping -c 1 SERVER > /dev/null 2>&1
if [ $? -ne 0 ]
then
  # bail out
fi
# do the job

I used the bellow to check the remote server is up and accepting remote login ( here i checked if telnet port is open or not.

MACHINE=8.8.3.200 # Change to what ever you need
exec 3>/dev/tcp/${MACHINE}/23
if [ $? -eq 0 ]
then
    echo "Server is up Telnet accepting connections"
else
    echo "Telnet connections not possible"
fi

Dr. House is at it again! Thanks for the example, once again. It appears I have more reading to do though, as there re some bits that I don't understand. The '2>&1'... that sends stderr to the same spot as stdout, right? In this case, /dev/null... But I am foggy about the '$?'. I havent used that one before...

---------- Post updated at 10:40 AM ---------- Previous update was at 10:38 AM ----------

Thank you very much for that example as well! It looks like I have two options, one to see if the machine is up, and one to check if the service itself is up. Thanks again!

... is merely a "geek's quicky", meant to keep the shell clean, but completely irrelevant to the task at hand (- see this page for details).

... is meant to inform you about success or failure of the preceeding command, i.e. your pinging of a server (- see this page for details).

Is it possible to incorporate this into a while loop, so that it would ping every 5 seconds or so, and if '$?' is -ne to 0, keep pinging until $?= 0 (then once it does equal 0, continue with the script)? I came up with one sample, but of course, it didn't do anything, but give me a nice error :slight_smile:

#! /bin/bash

while [ true ]
do
  ping -c 1 $SERVER > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    echo "ping"
    exit
  else
    echo "pong"
    sleep 5
  fi
done

exit 0

Thanks for that! I didn't have the [ true ] portion in my mock up (which may explain why I never got it to work...). I like reading your responses; I always learn something. Thanks again, and I'll try not to be a burden for a while!

Word of warning...

Your server may respond to a ping request during the fsck - the network interface is initialized before advancing to the multi-user run level. You may find that the network interface may respond to ICMP echo requests, yet still be in single-user mode. (Your mileage may vary)

One way to test if multi-user services are up, is to attempt to telnet to port 22 (the ssh port). If the service is up, you should see something like:

root(me)@myhost2# telnet myhost1 22
Trying 192.168.1.101...
Connected to myhost1.
Escape character is '^]'.
SSH-2.0-Sun_SSH_1.1

If the service is not yet up, you should see something like:

root(me)@myhost2# telnet myhost1 22
Trying 192.168.1.101...
telnet: Unable to connect to remote host: Connection refused
  • Avron

Veeeery interesting... Thanks for that Avron. If that is the case, it would really defeat the whole purpose of this check. For the longest time, my basic script simply waited (via 'sleep') for a set time (that I measured for how long the server would take to boot). Howeve during
the first 'fsck', that time was no longer applicable and the script errored out. I appreciate the insight, as it may very well have errored out still. I'll do some testing and see what exact messages are kicked back when the service is ready or not and search stdout for those messages, basing the scripts behavior on those results. Thanks again!

---------- Post updated at 09:12 PM ---------- Previous update was at 07:28 AM ----------

Hello there, I tried the above and am wondering if I am missing something... While the server is booting, the script does as it should, and echoes 'pong' until the server answers back. The problem is that as soon as the server answers back, the 'exit' quits the script, not the just the loop. I am assuming that there must be some way to continue with the script after this loop, but... Of course, being an experimenter, I removed the 'exit', and it simply echoed 'ping' thousands of times after the server came up. Will I need to have the 'echo "ping"' dump to a file, then have the backup script search that file every few seconds, looking for the "ping"? I know there is probably a more elegant way to do it, but whatever works...

---------- Post updated at 09:40 PM ---------- Previous update was at 09:12 PM ----------

WOOHOO! I got it to work by putting a 'break' in place of the 'exit'. I guess I just hadn't read enough when I posted my last query. Thanks for the help!

** CLARIFICATION **
Just to clarify... the above code was entered into my script (I wasn't using it as a stand-alone script). So, I have a section of the script that runs Ether-wake to send a magic packet to the server, then the above loop to wait for the server to be available. After this loop finishes with a successful ping, the script continues with rsync and encryption. Good times...

My way (not made explicit - sorry for that):

#! /bin/bash

function doTheRealWork
{
  # whatever ;-)
}

while [ true ]
do
  ping -c 1 $SERVER
  if [ $? -eq 0 ]
  then
    doTheRealWork
    exit 0
  else
    sleep 5 # wait
  fi
done

exit 0

I haven't messed with functions at all. I like that example though. It looks like I have more reading to do... As I stated earlier, I always learn something from your replies :slight_smile: Thanks again for lending your experience!