Before I forget it again, a quick and dirty way to have a shell script do some stuff parallel (forking off and having the parent wait till they are all done)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
forked="/var/tmp/f${RANDOM}.tmp"
touch $forked
chmod 600 $forked
cat /dev/null > $forked
for a in `whatever`
do
something with $a &
echo $! >> $forked
done
len=`wc -l $forked|awk '{print $1}'`
checkpid=0
while [[ $len -gt 0 ]]
do
lastpid=$checkpid
checkpid=`head -1 $forked`
if [[ "$checkpid" -ne "$lastpid" ]]
then
echo "$len children left"
echo "Checking if $checkpid is done"
fi
if [[ ! -e /proc/${checkpid}/cwd ]]
then
grep -v $checkpid $forked > ${forked}.tmp
mv ${forked}.tmp $forked
echo "-> $checkpid is done"
echo ""
len=`wc -l $forked|awk '{print $1}'`
fi
sleep 1
done
rm ${forked}.tmp $forked
exit
|