Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am looking for very simple bash script that would allow me to launch a process a few times. What's essential to me is that after the processes terminate, everything will clean up automatically.

Intended usage:

bash multiplerun.sh 5 executable.sh parameters_to_executable

I could probably write it myself, but it would take me so long that I've decided to ask experts; I'm very little skilled at using *ix systems.

EDIT: Oh, and I forgot to add that I want to run them in parallel - running them one after another is pretty straightforward.

share|improve this question
    
Isn't there some tension between "quick and dirty" and "everything cleaned up automatically"? The task is pretty difficult if the processes work on files, btw... –  sr_ May 19 '12 at 16:23
    
They don't work on files - they're only used as dummy clients connecting to a server. –  Bartek Banachewicz May 19 '12 at 19:34

2 Answers 2

up vote 4 down vote accepted

Here is something that will launch your programs in parallel:

#!/bin/bash

count=$1
command=$2
shift 2

for ((i=0;i<count;i++)); do
  $command "$@" &
done
wait
echo done

The wait instruction should prevent zombies to show up. If your programs do not need a tty, you can replace the loop with:

for ((i=0;i<count;i++)); do
  nohup $command "$@" </dev/null >/dev/null 2>&1 &
done

That way, the processes will be detached from your shell.

share|improve this answer
    
As for the cleanup - I meant that there won't be any zombie processes, screens or anything running. I'm gonna run the script more than once and I didn't want to end up with 1000 zombies just sitting there. –  Bartek Banachewicz May 19 '12 at 19:35
    
Answer updated. –  jlliagre May 19 '12 at 20:15
    
Thanks a lot ;) –  Bartek Banachewicz May 19 '12 at 20:31

If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:

seq 5 | parallel -N0 executable.sh parameters_to_executable

You can install GNU Parallel simply by:

wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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