0

I am using nested shell scripts.

My question is a bit similar to the ones asked here and here. But not exactly the same.

I have tried to get the solution from these but unsuccessful.

In my OuterMostShellScript.sh, I do something like this:

some commands
./runThisScriptX.sh
other commands
end of script.

runThisScriptX.sh contains a loop running some processes in the background by using & operator.

I want each process started by the ./runThisScriptX.sh command finish before the control moves to the, which i say other commands line in the above code.

how to achieve this?

EDIT: I also did like this:

some commands
./runThisScriptX.sh
wait
other commands
end of script.

but it did not work.

Community
  • 1
  • 1
tod
  • 1,539
  • 4
  • 17
  • 43
  • 2
    remove the `&` from the script? – Karoly Horvath Apr 14 '14 at 17:42
  • No I can not. I want them take the advantage of parallel/multicore. Which they already are taking... – tod Apr 14 '14 at 17:43
  • possible duplicate of [How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?](http://stackoverflow.com/questions/356100/how-to-wait-in-bash-for-several-subprocesses-to-finish-and-return-exit-code-0) – a5hk Apr 14 '14 at 17:45
  • @KarolyHorvath I have used `wait` but did not work – tod Apr 14 '14 at 17:49
  • @KarolyHorvath I have added some edit part in the question... – tod Apr 14 '14 at 17:54

4 Answers4

4

Two things:

  • Source your script
  • Use wait

Your script would not look like:

some commands
. ./runThisScriptX.sh          # Note the leading . followed by space
wait                           # This would wait for the sourced script to finish
other commands
end of script
devnull
  • 118,548
  • 33
  • 236
  • 227
1

Use the wait built-in command:

wait

This waits for all background processes started directly by the shell to complete before continuing.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • If the script is started with `./` and without `&`, there's nothing that can be done until the sub-script finishes. No command can do anything unless the sub-processes are run in the background. The command run via `./` can do the waiting; or as has been discussed, the script will have to be sourced. If there's still an issue with variables, maybe the answer is `( . ./Whatever; wait)`. But the fundamental answer remains `wait` — the only residual issue is deciding where to use it. – Jonathan Leffler Apr 14 '14 at 17:51
1

Use the bash built-in wait; from the man page -

Wait for each specified process and return its termination status. Each n may be a process ID or a job specification; if a job spec is given, all processes in that job's pipeline are waited for. If n is not given, all currently active child processes are waited for, and the return status is zero. If n specifies a non-existent process or job, the return status is 127. Otherwise, the return status is the exit status of the last process or job waited for.

Or, don't background the tasks.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Inside runThisScriptX.sh, you should wait for the parallel children to complete before exiting:

child1 &
child2 &
child3 &
wait

Then in OuterMostShellScript.sh, you run runThisScriptX.sh itself in the background, and wait for it.

some commands
./runThisScriptX.sh &
wait
other commands
end of script.

wait can only be used to wait on processes started by the current shell.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Works fine. if i do not run `./runThisScriptX.sh` itself in the background, then only the wait inside this script will be enough, right? – tod Apr 15 '14 at 16:27
  • 1
    Correct. The `wait` prevents `runThisScriptX.sh` from continuing (and thus exiting) until the child processes complete. – chepner Apr 15 '14 at 16:49