How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?

后端 未结 30 3050
悲哀的现实
悲哀的现实 2020-11-22 03:50

How to wait in a bash script for several subprocesses spawned from that script to finish and return exit code !=0 when any of the subprocesses ends with code !=0 ?

S

30条回答
  •  情书的邮戳
    2020-11-22 04:10

    I needed this, but the target process wasn't a child of current shell, in which case wait $PID doesn't work. I did find the following alternative instead:

    while [ -e /proc/$PID ]; do sleep 0.1 ; done
    

    That relies on the presence of procfs, which may not be available (Mac doesn't provide it for example). So for portability, you could use this instead:

    while ps -p $PID >/dev/null ; do sleep 0.1 ; done
    

提交回复
热议问题