4

I want to execute a process on multiple servers. I am using the following wrapper script. The script moves to servers one at a time. I would like to change it so it can execute on multiple servers. Is there way to do it?

#!/bin/bash
for ip in $(<ALL_SERVERS_IP); do
    # Tell the remote server to start bash, but since its
    # standard input is not a TTY it will start bash in
    # noninteractive mode.
    ssh -q "$ip" bash <<-'EOF'
        printf "%s\n" ================== "$(hostname) ::: Current date : $(date)" \
                            ==================
        ./remote 
    EOF
done
3

3 Answers 3

2

Yes. Try pdsh, which will execute a command on multiple remote hosts simultaneously. Your task could be written like so:

pdsh -w ssh:$(echo $(<ALL_SERVERS) | sed -e 's/ /,/g') 'printf "%s\n" ...'
2

Sounds like you're looking for GNU Parallel. That's pretty much exactly what it was written for. In your case, you would do:

parallel ./remote --sshloginfile ALL_SERVERS_IP

The above assumes that ALL_SERVERS_IP is a text file with one IP or hostname per line. If they can have different logins, make the list like so:

[email protected]
[email protected]
[email protected]
5
  • I checked on the central server and couldn't find parallel on it.
    – user67186
    Aug 21, 2014 at 17:53
  • @user67186 you only need to install it on your machine, the one you will launch the job from.
    – terdon
    Aug 21, 2014 at 17:55
  • Use a bash function to give the 'printf/hostname' stuff to run. Then use --env to export that function to the remote server: parallel --env myfunc --slf ALL_SERVERS_IP --nonall myfunc
    – Ole Tange
    Aug 22, 2014 at 7:08
  • @terdon How do you have to install parallel? Can't find anything useful.
    – Jenson
    Jan 27, 2016 at 7:34
  • @Jenson see the link in my answer. Most Linux distributions (probably all) will have it in their repositories as parallel. For others, the link provides download packages.
    – terdon
    Jan 27, 2016 at 10:32
0

I know some tools for that, but the easiest is Fabric. Take a look and tell me what do you think.

2
  • All anwsers are useful but I can choose one solution. Thanks!
    – user67186
    Aug 22, 2014 at 13:18
  • You're welcome man. I'm glad to help. Aug 22, 2014 at 20:17

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