I am task with something I have never done before.

I am sure is easy for some of you :)

I need to execute a linux shell script from our network that will connect ssh to this devices and reboot them with 15 minutes difference.

I imagine it will look something like this:


HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"

Need to connect the reboot command with the ssh connection

for p in server1 server2 server3
  ssh ${p} sudo reboot
  sleep 900 

I need it to connect and reboot after 15 minutes from the first machine rebooted.

  • If everything's set up correctly it looks like your second example should work. What results are you getting? – RobertL Nov 14 '15 at 3:57

What you are asking basically is something like this.

for i in `cat ~/servers.txt`
    echo -n $i" "
    ssh -l sudo_user $i "sudo shutdown -r now"
    sleep 900

Mind you that with the sleep you are issuing the reboot aprox 15 minutes after you made the previous order, and not 15 minutes after it finishing rebooting. What you need can be done with a simple bash shell, I would prefer using Rundeck if we are talking about a few servers, or Ansible if we are talking about more than 100.

The script is also best run using a sudo user; it is a good practice not to use root for current administration, and much less over ssh. For it to work seamlessly you would have to populate the user beforehand with the correct ssh RSA keys on each server, and add it to the sudo group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.