Simple parallel processing with Bash using “wait” and “jobs”
#!/bin/bash
# this is a simple helper script that can be used when you are in need of running multiple processes at once
# simply run your command with an ampersand at the end and after this
# execute forky with the number of parallel processes as argument.
function forky() {
local num_par_procs
if [[ -z $1 ]] ; then
num_par_procs=3
else
num_par_procs=$1
fi
while [[ $(jobs | wc -l) -ge $num_par_procs ]] ; do
sleep 1
done
}
# below is an example - make sure to change this to your needs
for fname in `ls -1`; do
echo "`jobs | wc -l` jobs in spool"
echo -e "processing $fname\n"
# below should be the command you like to execute. in this example it's a php script that would do something with a file.
# important is the & at the end of the command which sends the command to the background
php ~/a_script_that_does_something_with_the_file --file=$fname &
# after this call the forky function with the amount of parallel processes as parameter
forky 5
done
wait
sourcebench 1:53 pm on February 1, 2010 Permalink |
Simple parallel processing with Bash using “wait” and “jobs” – http://bit.ly/92T7jY
This comment was originally posted on Twitter