[SOLVED] How to run several commands on a Linux system in parallel mode? – check 1 simple solution

You are currently viewing [SOLVED] How to run several commands on a Linux system in parallel mode? – check 1 simple solution
Share This Post, Help Others, And Earn My Heartfelt Appreciation! :)
4.8
(173)

Run several commands

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!";

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!

If you enjoyed this post please add the comment below and share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments