2

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.

2
  • 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... Commented May 19, 2012 at 16:23
  • They don't work on files - they're only used as dummy clients connecting to a server. Commented May 19, 2012 at 19:34

2 Answers 2

4

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.

2
  • 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. Commented May 19, 2012 at 19:35
  • Answer updated. Commented May 19, 2012 at 20:15
1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.