Run a pool of processes in shell

I'm looking for an easy method to run N selected processes at the same time with one command. It should put all the output on my terminal and shut down all of them when I exit with ctrl+c. Is there any existing app that does this?

I'm thinking of some thing like exec_many 10 foo - it should keep 10 foos running and respawn any that dies.


Solution 1:

I don't know of one off hand, but you could do this with Bash without too much work. I would put each of the foo processes in a process group. You can then trap SIGINT in the parent and kill the process group with kill -pgid (Negative before process group number). You can also launch them all as jobs. So they all run at once (Pretty much). Finally, you could loop over the ouput of jobs every x seconds (sleep in the loop) and get the count, if the count is less than the number of foo processes, than you can fire up another one (Could get more fancy by making sure none are stopped etc).

A rough Version might be something like:

#!/bin/bash

command=$1
n_job=$2

function kill_jobs {
    echo traped
    for job in $(jobs -p); do
        echo killing $job
        kill $job
    done
} 

trap 'kill_jobs; exit' SIGINT

while true; do
    current_jobs=$(jobs -pr)
    x=0;
    for job in $current_jobs; do
        (( x++ ))
    done;

    jobs_to_run=$(($n_job - $x))

    for (( y = 0; y < $jobs_to_run; y++ )); do
        $command &
    done
    x=0 
    sleep 5
done

You can also find similar sort of stuff in the "Proccesses and Concurrency" section of Pythton for Unix and Linux System Adminsitration.

Solution 2:

From synaptic....

build and execute command lines from standard input in parallel

GNU Parallel is a shell tool for executing jobs in parallel using one or more machines. A job is typically a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, or a list of tables.

If you use xargs today you will find GNU Parallel very easy to use. If you write loops in shell, you will find GNU Parallel may be able to replace most of the loops and make them run faster by running jobs in parallel. If you use ppss or pexec you will find GNU Parallel will often make the command easier to read.

GNU Parallel also makes sure output from the commands is the same output as you would get had you run the commands sequentially. This makes it possible to use output from GNU Parallel as input for other programs.