You’ve got a dual quad core system and you want to run jobs on all eight “processors” in a bash shell script. This is how you can do it:
maxjobs=8
parallelize () {
while [ $# -gt 0 ]; do
count=(`jobs -p`)
if [ ${#count[@]} -lt $maxjobs ]; then
runstuff $1 &
shift
fi
done
wait
}
parallelize argv1 "argv2-1 argv2-2b" argv3 ... argvn
There are also utilities already written just for this type of thing, but forget xargs. Check out xjobs instead.
xjobs reads job descriptions line by line and executes them in parallel. It limits the number of parallel executing jobs and starts new jobs when jobs finish. If no utility is given as an argument to xjobs, then the first argument on every job line will be used as utility. xjobs support I/O redirection, which makes some applications possible that cannot be done with GNU’s xargs. xjobs also determines the number of processors automatically, whereas xargs must be told how many processes to start.
6:28 pm
Parallel https://savannah.nongnu.org/projects/parallel/ does not pollute the output as much as xjobs and sends STDERR of the jobs to STDERR.
xjobs does not deal nicely with input having weird characters (such as “), whereas Parallel does.