-1

For example, I have an executable called ex01 (C++ program).

And I have a text file that contains a set of invocations of this program with different arguments.

The text file looks like this:

./ex01 command1 > ex.dat  &
./ex01 command2 >> ex.dat &
./ex01 command3 >> ex.dat &
...

I can use the source command to do this task automatically.

But I was hoping to do this in parallel. How can I do that?

3
  • What makes you think that the source command has anything to do with parallelism? Commented Jul 18, 2013 at 20:49
  • 1
    I believe he is sourcing the file to run a bunch of commands. He wants to run the commands in parallel. Commented Jul 18, 2013 at 21:09
  • 1
    Could you be a bit more descriptive in your first sentence? I really don't get it. C++ source code doesn't really generate anything. Commented Jul 18, 2013 at 21:18

1 Answer 1

4

Instead of sourcing the file, you should run it in a separate shell. The source command will run the script's contents in the current shell.

$ cat script.sh
./ex01 command1 > ex.dat;
./ex01 command1 > ex.dat;
...
$ sh script.sh
 ...

The semicolons are not necessary.

./ex01 command1 > ex.dat
./ex01 command1 > ex.dat
...

To run the commands in parallel, add an ampersand (&) to the end of each command.

./ex01 command1 > ex.dat &
./ex01 command1 > ex.dat &
...

Note that the above will be problematic because two commands are writing to a single file. You should either write to different files,

./ex01 command1 > ex1.dat &
./ex01 command1 > ex2.dat &
...

or append to the file.

./ex01 command1 >> ex1.dat &
./ex01 command1 >> ex2.dat &
...

If your script repeats the same line over and over again, you can use a loop.

# Run 10 times
for i in $(seq 1 10) ; do
    ./ex01 command1 >> ex1.dat &
 done

Edit: If the argument's number changes linearly, you can use a loop like this:

# Run 10 times
for i in $(seq 1 10) ; do
    ./ex01 command$i >> ex.dat &
done
1
  • Thank you, You give me a lot of hints. Some part is modified by other guy who misunderstood my meaning. For each command line is different. I would like to do some work of bifurcation. But if for example 10,000 command is executed at the same time by command &. The computer is beyond the memory and cpu. Commented Jul 22, 2013 at 18:59

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.