3

I want to know the good practice of performing a series of commands simultaneously in UNIX/Linux. Suppose that I have a program, program_a, which requires one parameter. I have stored parameters line by line in a file. So I wrote:

while read line
do
    ./program_a line > $line.log 2>&1
done < parameter_file

The problem is that execution of program_a takes long time. Because each executions of program_a for each parameter is independent, So I think these executions can be run simultaneously. I don't know if it regards to multithreading or other technique. The following is my thought. Use & to run each executions on the background.

while read line
do
    ./program_a line $line.log 2>&1 &
done < parameter_file

Is there any better way of launching multiple tasks?

7
  • Your current way seems pretty good. Commented Dec 2, 2013 at 11:22
  • 2
    That's the standard way to do it. You can use the wait command to hold up proceedings until all the child processes are complete. There are programs like parallel that you could use too. Commented Dec 2, 2013 at 11:22
  • What do you mean by 'better' exactly (ie what properties might a 'better' way have)? This is a normal and useful technique, is it giving you issues? Commented Dec 2, 2013 at 11:22
  • 3
    Use GNU Parallel. Commented Dec 2, 2013 at 11:23
  • I believe you want to >> line.log instead of > line.log (overwriting previously written infos) ? and $line instead of line Commented Dec 2, 2013 at 11:35

2 Answers 2

5

Did you know that xargs can launch tasks in parallel? Check out -P -n parameters!

An example:

xargs -P 4 -n 1 ./program_a < parameter_file

That will start up to 4 (P=4) program_a instances for processing each line (n=1). You'll probably have to wrap program_a within a shell script or something so that child processes stdout & stderr can be redirected appropriately.

How this is better than putting processes to backgroud: Suppose you have 1000 lines in the input file, obviously you wouldn't want 1000 processes to be launched. Xargs allows you to look at it as a queue, with P workers each consuming and processing n items from it.

Sign up to request clarification or add additional context in comments.

Comments

1

With GNU Parallel you can get a logfile for each parameter and run one job per CPU core:

parallel --results logdir ./program_a :::: parameter_file

Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

Walk through the tutorial (man parallel_tutorial). You command line with love you for it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.