0

I have the following code that submits several subjobs withing a docker container:

#!/bin/bash

for file in "all files of a given type"; do 
     docker exec -itd "docker container" "command to be executed within docker container" &
done

pidlist=$(pgrep -f "command to be executed within docker container")

for pid in $pidlist; do
    echo $pid
    wait $pid
done

My goal is to have the script wait to exit until all the subjobs are done. I need this since this script is part of a larger script containing other commands and scripts.

However, the PIDs that I get are not the same as when I use top at the terminal to locate the subjobs and so, the script exits after it has submitted all the subjobs.

2
  • You're forking twice: with -d and with &. Also I'm not sure it's a good idea to use the option -t (and keeping -i would require something else than the terminal as input too). – A.B Sep 14 '20 at 13:29
  • thanks for the info – dtx1 Sep 14 '20 at 19:32
0

You don't need to wait on explicit pids; if all your script is doing is tarting a number of background processes, you can just wait for them all to complete. Try something like this:

#!/bin/bash
for file in "all files of a given type"; do 
    docker exec -itd "docker container" "command to be executed within docker container" &
done

wait

This modified version of your script starts some number of background processes, then waits for all of them to terminate.

3
  • Doesn't a single wait wait for all children? – G-Man Says 'Reinstate Monica' Sep 17 '20 at 5:01
  • @G-ManSays'ReinstateMonica' doh -- my mistake, thank you. – Andy Dalton Sep 17 '20 at 12:12
  • Thanks for the info – dtx1 Oct 1 '20 at 2:47

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.