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.

link|improve this question

64% accept rate
feedback

1 Answer

up vote 2 down vote accepted

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.

link|improve this answer
Actually, not sure how to specify process group from the shell, but you could keep an array of the pids. – Kyle Brandt Apr 21 '10 at 11:39
There's a done missing in kill_jobs. Otherwise - works great. – viraptor Apr 21 '10 at 13:12
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.