I have a list of servers in a file that I pass to the following script:
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
ssh_cmd="user@$line"
ssh $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token' &
done < "$1"
What I want is output like this:
output from hostname (1)
output from ls -l (1)
output from hostname (2)
output from ls -l (2)
output from hostname (3)
output from ls -l (3)
But these seem to be mixed up:
output from ls -l (1)
output from hostname (1)
output from hostname (2)
output from ls -l (2)
output from ls -l (3)
output from hostname (3)
Is there some way to ensure that these commands happen sequentially?
Thanks in adv!
The &
at the end of this line makes it so the process runs in the background:
ssh $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token' &
If you remove the &
from that line, the script will wait until the ssh session finishes to continue on to the next iteration of the loop. Also, you will need to add the -n
flag to the ssh command so that it doesn't read from stdin. This will give you the sequential output you're looking for.
Final command:
ssh -n $ssh_cmd 'hostname && ls -l /path/to/folder | grep some_token'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments