3

I have a small script to install on my remote machine an update.

I would like to reboot the machine remotely and if it is rebooted, continue with some more commands. What I currently do is:

ssh root@myMachine << COMMANDS_ISSUED
   ###... Tasks
   init 6
COMMANDS_ISSUED
sleep 180s
ssh root@myMachine << POST_REBOOT_COMMANDS
   ###.... More stuff
POST_REBOOT_COMMANDS

Is there a more elegant way to do it? Like pinging the machine all 5 seconds up to a maximum of 4 minutes? I play with a few linux machines which have different boot up times and if my script would continue immediately after reboot, this could safe quite some time for me. (Note: I don't want to parallelize execution over all machines as I want to see for each machine if everything worked fine)

2 Answers 2

5

I would say ping the machine until it is up.

while ! ping -c 1 192.168.1.1 &>/dev/null; do :; done

"c 1" will send one request at a time, so one each loop. If there is no response ping will return false and the loop will continue.

The body of the loop is empty - you could put a short sleep command in there if you want. Otherwise, the colon ":" which is the [no-op command][1], is used. This is because you can't have an empty loop body.

1
  • Despite the fact that I need to use ping -n instead of ping -c this works like a charm, thanks :)
    – Theo
    Commented Oct 28, 2013 at 10:42
-1

you can define a crontab like this:

@reboot script-to-execute

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .