简体   繁体   中英

How to know the process numbers of all the background commands running in parallel

I'm writing a shell script to run various processes in the background. I want to know how can I wait the execution of the script until all the background processes (which are running in parallel) have completed.

#!/usr/bin/env bash

ARRAY='cat bat rat'

for ARR in $ARRAY
do
    ./run_script1 $ARR &
done

P1=$!
wait $P1

echo "INFO: Execution of all background processes in the for loop has completed.."

Here I've used the variable $! which gives the process number of the last background command, but i want to have the process number of every background command running in parallel.

How can this be achieved?

If you want to wait for all children, you don't need to know their pids, just call wait without arguments.
From wait --help :

If ID is not given, waits for all currently active child processes, and the return status is zero.

But if you want to list pids of all children for some reason, you can use:

jobs -p

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM