Linux/Basic commands/parallel/Execute a command in parallel by ssh


  1. Execute a command in parallel by ssh in MACHINE1 and MACHINE2 using parallel
parallel ssh {} 'COMMAND' ::: MACHINE1 MACHINE2[1]. See also pssh.
parallel -q ssh {} 'ECHO "TEST"; COMMAND' ::: MACHINE1 MACHINE2[2]
-q quote command. The command must be a simple command (see man bash) without redirections and without variable assignments. This will quote the command line and arguments so special characters are not interpreted by the shell.

Using variables:

parallel -q ssh {} "echo {}; $COMMAND" ::: $MACHINES

or creating a basic shell script:

#!/bin/bash
MACHINES="$1"
COMMAND="$2"

if [ $# -eq 0 ]; then
    echo -e '\nUSAGE: $0 "machine1 machine2 machine3" "command"\n'
    exit 0
fi

parallel --keep-order -q  ssh {} "echo {}; $COMMAND" ::: $MACHINES


You can have a similar solution with a for loop but in this case command output will be mixed between both commands. for HOST in HOSTNAME1 HOSTNAME2; do ssh $HOST "COMMAND" & done

See also

edit
  1. https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang
  2. https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang