This is a simple script which runs nvidia-smi command on multiple hosts and saves its output to a common file. The goal here is to make it run asynchronously.

Is & at the end of the process_host() function call sufficient? Is my script correct?


HOSTS=(host1 host2 host3)


process_host() {
    echo "Processing" $host
    output=`ssh ${host} nvidia-smi`
    echo ${host} >> $OUTPUT_FILE
    echo "$output" >> $OUTPUT_FILE

for host in ${HOSTS[@]}; do
    process_host ${host} &


Yes, but your output may be garbled. It's better to have the function write its output to a specific file depending on the host name and then let the main script concatenate the result (and clean up).

Also, you should double quote your variables. Copy and paste the script into ShellCheck.

Maybe something like this:


hosts=( host1 host2 host3 )

rm -f "$outfile"

function process_host {
    local host="$1"
    local hostout="$host.out"
    printf "Processing host '%s'\n" "$host"
    echo "$host" >"$hostout"
    ssh "${host}" nvidia-smi >>"$hostout"

for host in "${hosts[@]}"; do
    process_host "${host}" &


for host in "${hosts[@]}"; do
    cat "$hostout"
    rm -f "$hostout"
done >"$outfile"

cat "$outfile"

The last loop may be replaced by

cat "${hosts[@]/%/.out}" >"$outfile"
rm -f "${hosts[@]/%/.out}"
  • I get exactly the output I want to get, what could go wrong here? – syntagma Aug 9 '16 at 11:10
  • 1
    @ΔλЛ If, for example, the first host is slow to respond, Processing host1 will be followed by Processing host2 and the output from host2 rather than the output from host1. – Kusalananda Aug 9 '16 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.