-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?

  • What makes you think that the source command has anything to do with parallelism? – a CVn Jul 18 '13 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. – user26112 Jul 18 '13 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. – jcora Jul 18 '13 at 21:18
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
  • 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. – Jimmy Wang Jul 22 '13 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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