0

I have 10 servers and I access them using jumpbox. I wrote a script for starting a service on each server. When I execute that script it's only run on one server and after pressing Ctrl+C it's executed on the next server and so on. I want to run it on all servers without pressing Ctrl+C.

This is my script:

#!/bin/bash
for i in `cat /etc/hosts | grep prod | awk '{print $2}'` ;
do
ssh $i "hostname && sudo service name start"
done;
4
  • 1
    Make sure your user is allowed to execute sudo service name start without entering a password. Commented Oct 22, 2016 at 18:20
  • @Cyrus yes no issue with sudo Commented Oct 22, 2016 at 18:42
  • I guess there is a problem with output redirection in your service script. Commented Oct 22, 2016 at 20:53
  • I, too, think you''re problems depend standard file redirections. You could try to add option -n to ssh and to end the command-line with &. As a last comment, you can replace the three sub-processes cat /etc/hosts | grep prod | awk '{print $2}' with just one sub-process awk '/prod/ {print $2}' /etc/hosts. Commented Oct 26, 2016 at 12:18

1 Answer 1

1

Use & to fork new process.

#!/bin/bash
for i in `cat /etc/hosts | grep prod | awk '{print $2}'` ;
do
ssh $i "hostname && sudo service name start" &
done;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.