I'm trying to start multiple processes at the same time using a for loop. I want each to be created in it's own console window. So far i have the following:

for i in {1..5}; do konsole -e "./program arg1 arg2"; done 

However , this simply runs them one after another, where i want them to run concurrently. I've tried using '&' but i can't get i to play nicely with the loop.

  • 3
    Replace the ; before done with &. But first check whether konsole -e "./program arg1 arg2" does what you think it does. – AlexP Dec 14 '18 at 23:20
  • 2
    @AlexP You should write that up as an answer. I would have upvoted. – Sparhawk Dec 15 '18 at 3:44

A background task is started by putting a & at the end of the command. Your loop would start konsole in the background if you wrote it as

for i in {1..5}; do
    konsole -e "./program arg1 arg2" &

or, on a single line,

for i in {1..5}; do konsole -e "./program arg1 arg2" & done

The & is a command terminator, just like ; (and newline and a few others), so it replaces the ; that you originally had there.

Would you want the script to wait for the started background tasks to finish, add a wait at the end:

for i in {1..5}; do konsole -e "./program arg1 arg2" & done; wait
| improve this answer | |

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.