Use multiple processors in bash by running commands in parallel

Posted in Linux

dual_quad_core_processorsYou’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:

#!/bin/bash

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

xargs vs xjobs

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.

Posted by admica   @   15 January 2010

Related Posts

1 Comments

Comments
Jan 27, 2010
6:28 pm
#1 Ole Tange :

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.

Leave a Comment

Name

Email

Website

Previous Post
« Navigating Complex Mazes: Lab Rats vs Mineral Oil
Next Post
How to make a ninja mask with a tshirt »
Powered by Wordpress   |   Lunated designed by ZenVerse