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)

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

Related posts:

  1. Bash scripting, traps and sleep
  2. Script of the day – clean up stale .ssh/known_hosts
  3. Mythtv and IR Blaster
  4. Bash Scripting
  5. bash scripting … switching into directory of the script

Trackback URL

No Comments on "Bash scripting"

Hi, leave a comment:


Connect with Facebook

ALLOWED XHTML TAGS:

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

Subscribe to Comments
Performance Optimization WordPress Plugins by W3 EDGE