Bash scripting

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)

Select All Code:
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

Trackback URL


No Comments on "Bash scripting"

Hi, leave a comment:


ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Subscribe to Comments