How to start multiple processes in Bash

I want to start 100 processes in bash, but the for statement doesn't seems to like the & symbol and I'm getting a syntax error, here is what I have so far:

echo "Spawning 100 processes"
for i in {1..100}
do
    ./my_script.py &
done

EDIT: I was copypasting this code, that's why the & char was illegal.


echo "Spawning 100 processes"
for i in {1..100} ;
do
    ( ./my_script & )
; done

With GNU Parallel you can do:

echo "Spawning 100 processes"
parallel -j0 ./my_script.py ::: {1..100}

Or to avoid the argument 1 through 100:

parallel -j0 -N0 ./my_script.py ::: {1..100}

Without -j0 it will spawn one process per CPU thread.

Watch the intro videos for more details: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1


The only reason I can think of why this wouldn't work is if you were really using some other shell, like /bin/sh.

Do you have #!/bin/bash at the top of your file? If yes, please change it to #!/bin/bash -x (to turn on tracing, or xtrace as it's called in the manual page) and paste the relevant output into your question, along with the exact syntax error that is occurring. If no, that might be your problem. ;-)

The other possibility I can think of is if you have ^M characters (DOS line endings) in your file, which might result in errors such as the following (depending on which line they are on, if they are scattered around, or depending on if the script starts with a #! line):

-bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory
'/myscript.sh: line 2: syntax error near unexpected token `do

This page has a nice perl snippet that can remove them, as follows (which I have modified slightly so it will work in the unlikely case that you have a stray ^M in the middle of a line):

perl -pi -e 's/\r//g' myscript.sh