You are currently viewing [SOLVED] How to run several commands on a Linux system in parallel mode? – check 1 simple solution
Could You Please Share This Post? I Appreciate It And Thank YOU! :) Have A Nice Day!
4.8
(173)

Run Several Commands In Parallel

You would like to run multiple commands at the same time, but each of them should be run in a separate thread. The following script will allow you to do this.

#!/bin/bash

for cmd in "$@"; do {
  echo "--> Running \"$cmd\" command!";
  $cmd & pid=$!
  RUN_PID_LIST+=" $pid";
} done

trap "kill $RUN_PID_LIST" SIGINT
echo "--> Commands were run...";
wait $RUN_PID_LIST
echo "--> All your processes have been completed!";
[SOLVED] How to run several commands on a Linux system in parallel mode? - check 1 simple solution
bash run multiple commands in parallel
Bash Run Multiple Commands In Parallel

Linux Run Multiple Commands In Parallel

If you have ever wanted to run multiple commands in parallel, the Bash allows you to achieve just what you need. This command allows you to run multiple commands at the same time, and is perfect for when you need to run multiple tasks simultaneously.

Then run the command running Bash script:

./run_parallel.sh "echo 1" "sleep 2" "echo 2" "sleep 2"

You will get the result as below:

./run_parallel.sh "echo 1" "sleep 2" "echo 2" "sleep 2"
--> Running "echo 1" command!
--> Running "sleep 2" command!
1
--> Running "echo 2" command!
--> Running "sleep 2" command!
2
--> Commands were run...
--> All your processes have been completed!

Bash Run Multiple Commands In Parallel – Summary

I tried to create the simple example and I hope I did it 🙂 Please let me know if it was something what you were looking for?

Could You Please Share This Post? 
I appreciate It And Thank YOU! :)
Have A Nice Day!

BigData-ETL: image 7YOU MIGHT ALSO LIKE

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 173

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?