Difference between revisions of "Execute a command in parallel by ssh"
Jump to navigation
Jump to search
↑ https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang
↑ https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang
Line 5: | Line 5: | ||
:<code>parallel ssh {} 'COMMAND' ::: MACHINE1 MACHINE2</code><ref>https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang</ref>. See also [[pssh]]. | :<code>parallel ssh {} 'COMMAND' ::: MACHINE1 MACHINE2</code><ref>https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang</ref>. See also [[pssh]]. | ||
:<code>parallel -q ssh {} 'ECHO "TEST"; COMMAND' ::: MACHINE1 MACHINE2</code><ref>https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang</ref> | :<code>parallel -q ssh {} 'ECHO "TEST"; COMMAND' ::: MACHINE1 MACHINE2</code><ref>https://unix.stackexchange.com/questions/428997/on-which-host-does-this-parallel-command-hang</ref> | ||
− | ::-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. | + | ::<code>-q</code> 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: | Using variables: | ||
:<code>parallel -q ssh {} "echo {}; $COMMAND" ::: $MACHINES</code> | :<code>parallel -q ssh {} "echo {}; $COMMAND" ::: $MACHINES</code> |
Revision as of 05:04, 29 September 2020
- 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
- Ansible
xargs
,parallel
,paste
,map
- SSH clients:
ssh (OpenSSH)
,putty
, MobaXterm,plink
,mosh
,paramiko
,conch
pssh
,sshpass
andautossh
,sftp, Termius
Advertising: