0

This is my current script which runs another script on multiple files (filename0, filename1, etc). My problem is that it takes time to run and so would like to run them in parallel. Most solutions involve backgrounding the jobs. However if I do this then the job just stops. The way I get round this at the moment is to run this command in different terminal windows in batches of 10.

for i in `seq 0 100` 
do
my-other-script filename$i
done
1
  • Please don't wield the ancient seq command. It is older than time. Use for i in {0..100} to iterate over numbers. And as a constant reminder: always quote parameter expansions. Using "filename$i" would be safer and you wouldn't have to worry about filenames containing whitespace. Commented Jun 6, 2013 at 11:41

2 Answers 2

2

Jobs don't "just stop" because they're backgrounded, but you have to wait for them to finish to avoid exiting the script before they're done. Documentation

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

4 Comments

If I do a CTRL-Z to suspend the job it says 'stopped' instead of 'Suspended' which is the problem I am having
Please read the documentation. If you stop it you can either fg it or bg it, both leading to continuing execution.
I cannot bg the job. If I do 'my-script filename1 &' I do not get a command line back, the job stops completely. It disappears from my 'jobs' list.
@user1958508 Your job is stopping because it is done. All of its children are happily running in the background, but they are not children of your current shell so they don't appear in the jobs list. Your script should spawn its children in the background and then wait for them to finish.
0

GNU Parallel is made for precisely these kind of tasks:

seq 0 100 | parallel -j10 my-other-script filename{}

You can find more about GNU Parallel at: http://www.gnu.org/s/parallel/

You can install GNU Parallel in just 10 seconds with:

wget -O - pi.dk/3 | sh 

Watch the intro video on http://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

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.