[SOLVED] How To Run Several Commands On A Linux System In Parallel Mode? – Check 1 Simple Solution!

How to run several commands on a Linux system in parallel mode?
Share this post and Earn Free Points!

In this tutorial I will show you how to run several commands in parallel way in Bash.

Introduction

Parallelism

In computing, parallelism refers to the ability of a system or process to perform multiple tasks concurrently, rather than sequentially. This can be achieved in a number of ways, including:

  • Multiprocessing: This is the use of multiple processors or cores within a single computer to execute multiple tasks simultaneously.
  • Multithreading: This is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to execute multiple threads (smaller units of a process) concurrently, with each thread running on a separate core or processor.
  • Distributed computing: This is the use of multiple computers to perform tasks concurrently, often over a network.

Parallelism can be useful for improving the performance of certain types of tasks, such as those that can be broken down into smaller independent units of work. It can also be useful for increasing the reliability and fault tolerance of a system by allowing it to continue functioning even if one of the parallel tasks fails.

Bash

Bash is a Unix shell, which is a command-line interface for interacting with an operating system. It is the default shell on many Unix and Linux systems.

With Bash, you can run commands, execute scripts, and interact with the operating system using a command prompt. You can also use Bash to manipulate files, create directories, and perform other tasks that you would normally do using a graphical user interface (GUI).

Here are some basic Bash commands to get you started:

  • pwd: Print the current working directory.
  • ls: List the files and directories in the current working directory.
  • cd: Change the current working directory.
  • mkdir: Create a new directory.
  • touch: Create a new file.
  • mv: Move or rename a file or directory.
  • rm: Delete a file or directory.

To learn more about Bash and how to use it, you can refer to online resources such as the Bash manual or tutorials available on the internet.

Linux Run Multiple Commands In Parallel

Bash supports several ways to run commands in parallel, depending on your specific needs. Here are a few options:

  • &: You can run a command in the background by appending an & symbol to the end of the command. This allows you to run multiple commands at the same time, and you can use the fg and bg commands to bring background tasks back to the foreground or send them back to the background, respectively.
  • &&: You can run multiple commands sequentially by separating them with the && operator. This will execute the second command only if the first command succeeds (i.e., returns an exit status of 0).
  • &|: You can run multiple commands in parallel and wait for all of them to complete by using the &| operator. This is similar to the & operator, but it waits for all background tasks to complete before returning control to the shell.
  • xargs: You can use the xargs command to execute a command in parallel on multiple input arguments. For example, you can use ls | xargs -P 4 -n 1 command to run the command on each file in the output of ls, using 4 processes in parallel.
  • GNU Parallel: This is a command-line tool that allows you to execute shell commands in parallel on the local machine or a remote server. It is useful for running multiple instances of a command that takes input from a file or standard input.
# Run command1 and command2 in the background
command1 &
command2 &

# Wait for all background tasks to complete
wait

# Run command3 only if command1 and command2 succeed
command1 && command2 && command3

# Run command4 and command5 in parallel, and wait for them to complete
command4 &| command5 &| wait

# Run command6 on each file in the output of ls, using 4 processes in parallel
ls | xargs -P 4 -n 1 command6

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!

Summary

Bash Run Multiple Commands In Parallel

Parallelism can help speed up the execution of certain types of tasks that can be divided into smaller independent parts. It can also make a system more reliable and able to withstand failures, as it allows it to continue running even if one of the parallel tasks encounters a problem.

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!

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?