-2

I have the following shell script that executes multiple jar files from a folder. The script is working fine but I want the for loop to WAIT until a .jar file execution is complete before starting the execution of the next .jar file in the loop. Below is the code:

#!/bin/bash

for i in "/abc/pqr/"*
  do
    "$i" &
done
wait
jww
  • 97,681
  • 90
  • 411
  • 885
Shahid Sarwar
  • 1,209
  • 14
  • 29
  • 6
    Then why are you using `&`? – chrylis -cautiouslyoptimistic- Sep 04 '18 at 10:51
  • to make it keep executing in the background aswell – Shahid Sarwar Sep 04 '18 at 11:03
  • Possible duplicate of [Wait for bash background jobs in script to be finished](https://stackoverflow.com/q/1131484/608639), [How to wait in bash for several subprocesses to finish and return](https://stackoverflow.com/q/356100/608639), [Waiting for background processes to finish before exiting script](https://stackoverflow.com/q/14254118/608639), etc. – jww Sep 04 '18 at 11:05
  • 1
    This make no sense, why send a process in the background if you have to wait for it anyway? In your `for` loop, you could add an `if then else fi` with `ps -ef` to check if it is still running, but this is overkill IMHO. – Nic3500 Sep 04 '18 at 12:17

1 Answers1

1

Remove the & from inside this script and then run the parent script in the background instead.

./myScript.sh &

your for loop will execute the jars in turn, waiting for each one to complete before moving on. The entire process will run in the background.

user1717259
  • 2,717
  • 6
  • 30
  • 44
  • Hi, thank you for ur reply. Is there a way to automate this task? I mean could I achieve the same inside the script instead of using & with it while firing it? – Shahid Sarwar Sep 06 '18 at 06:35