Reputation: 10495
I would like to run n
processes (in my case simulations) simultaneously, so I'm using &
like so:
for i in {1..50};
do
run simulation &
done
After each simulation is done I need to parse its output files. My question is how do I know when a process is done, and what do I do to invoke a function upon finishing a process?
EDIT: I think what I need to do is call a function that would invoke the simulation and parse its files after it is done. The way I see it this function has to be a child process anyway, right?
Thanks!
Upvotes: 0
Views: 139
Reputation: 16758
The wait
builtin waits for all child to finish. If you want to execute a command for each jobs that finishes, you can use trap
. In your case, a combination of wait
and trap
should work:
trap parse_output SIGCHLD
for i in {1..50}; do
run simulation &
done
wait
Upvotes: 0
Reputation: 182063
As you don't seem to want/need to wait until all simulations are done, you can simply do this:
for i in {1..50};
do
{ run simulation && parse output; } &
done
Upvotes: 3
Reputation: 11252
Use the wait
builtin utility. See man bash
for more information. In the simplest case, wait
by itself will wait for all child processes to terminate.
Upvotes: 0