0

In my bash shell script, I wanted to fetch data in parallel from remote machines so I used a & in between the commands

Since my path was configured at runtime I first used a variable (String by default ) as such,

variable1="rsync -a /path/to/${runtime_parameter}_01/abc/xyz.txt"
variable2="rsync -a /path/to/${runtime_parameter}_02/def/ghi.txt"
variable3="rsync -a /path/to/${runtime_parameter}_02/mno/jkl.txt"

Then I execute them as follows,

`$variable1` & `$variable2` & `$variable3`

When using a single rsync I make a check verifying if $? i.e. exit status is equal to 0 for success.

My script should not proceed unless the rsync process is complete. How do I verify if the process are completed?

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
  • 1
    What you've written won't do what you think. You should be using: `$variable1 & $variable2 & $variable3 &` (with dollar signs prefixing the variable names, no back-ticks, and with an `&` after the third command. Then your main shell can use `wait`. If you need process IDs, you need: `$variable1 & p1=$! ; $variable2 & p2=$! ; $variable3 & p3=$!` or something equivalent (and the spaces after the `$!` are necessary because otherwise, `bash` says "`!: event not found`" — if I wanted a `csh`, I know how to get one and I dislike `bash` importing `csh`). – Jonathan Leffler Oct 04 '13 at 14:28
  • @JonathanLeffler Thanks for other corrections – Suvarna Pattayil Oct 04 '13 at 14:42

1 Answers1

4

You invoke wait after spawning all jobs in the background (even the last one). It will wait until they're all finished.

If you want to get the return status code from each individual child process, take a look at this answer: https://stackoverflow.com/a/356154/4323 (basically, use wait, but one PID at a time).

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436