Possible Duplicate:
Parallelizing a for loop

The original code might look like this:

 for i in *; do something.py $i; done

I was wondering whether I can run these jobs parallelly in the backgroud, such as:

 for i in *; do something.py $i &; done

I tried and found the & here won't work..

Moreover, a better way might be that bash allow 8 jobs(or any number) run together in the queue in background, but I don't know how to do that...

Does anyone have ideas about this? Thanks!

  • I would just do this in Python, anyway. – Keith Oct 31 '12 at 4:29
  • Not very much a duplicate, that one doesn't concern the problem with & though – daisy Oct 31 '12 at 8:49

Simply remove the ; character, so in final :

 for i in *; do something.py $i & done

And for running N instance of your script at the same time, see man 1 parallel

See http://www.gnu.org/software/parallel/

  • Added some hints about parallel – Gilles Quenot Oct 31 '12 at 3:12
  • 2
    It isn't necessary to comment after editing a post: its a wiki, the edit history is recorded... – jasonwryan Oct 31 '12 at 4:06
  • Interesting, you don't need the semicolon before done – daisy Oct 31 '12 at 8:48
  • & is a command terminator like ; and newline: also && and ||. See SHELL GRAMMAR in the bash manpage, or here – glenn jackman Oct 31 '12 at 13:40

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