How to spawn a background process in a bash script

For example, I want to spawn many ssh background processes in one bash script:

for i in `cat ./all-hosts`
do
    ssh $i "ifconfig | grep Link"
done

Simply adding a & to the ssh commands does not work.

asked Jul 23, 2013 by anonymous

1 Answer

 
Best answer

Here is the script that works:

for i in `cat ./all-hosts`
do
    ssh $i "ifconfig | grep Link" &
done

Or in one command

for i in `cat ./all-hosts`; do ssh $i "ifconfig | grep Link" & done

What is needed is to create a subshell.

answered Jul 23, 2013 by anonymous
edited Jul 23, 2013

The makes many processes run in parallel.

To wait for all processes to exit to continue next step:

wait

For the above script:

for i in `cat ./all-hosts`
do
    ssh $i "ifconfig | grep Link" &
done

wait
# continue for next steps
commented Jul 23, 2013 by anonymous
edited Jul 23, 2013

Please log in or register to answer this question.

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...