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 3: | Line 3: | ||
# [[Execute a command in parallel by ssh]] in MACHINE1 and MACHINE2 using parallel | # [[Execute a command in parallel by ssh]] in MACHINE1 and MACHINE2 using parallel | ||
− | :<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 <code>[[pssh]]</code>. |
:<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> | ||
::<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. | ::<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. |
Revision as of 06:20, 19 October 2022
- Execute a command in parallel by ssh in MACHINE1 and MACHINE2 using parallel
parallel ssh {} 'COMMAND' ::: MACHINE1 MACHINE2
[1]. See alsopssh
.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
parallel
,for
,xargs
,parallel --citation, sshlogin
xargs
,parallel
,paste
,map
- SSH clients:
ssh (OpenSSH)
,putty
, MobaXterm,plink
,mosh
,paramiko
,conch
pssh
,sshpass
andautossh
,sftp, Termius
Advertising: