Running Bash commands in parallel is a technique or method of executing multiple commands simultaneously in a Bash shell environment. This method helps in reducing overall execution time. In this article, I will discuss different ways to run multiple Bash commands in parallel. I will also show some practical examples with Bash scripts of parallel operations. So let’s start.
Key Takeaways
- Learning the necessity of running Bash commands in parallel.
- Learning multiple ways to run commands in parallel.
- Getting the overview of the whole process with practical Bash script examples.
Free Downloads
4 Ways to Run Bash Commands in Parallel
Read out the following described ways, through which you can run multiple Bash commands of a Bash script in parallel while executing the script.
1. Ampersand ‘&’ Sign
As a simple approach, use the inherent Bash ampersand (&) operator to run commands in parallel. It allows to execute a command asynchronously, enabling the shell to proceed to the next command without waiting for the current one to finish.
The basic syntax >
command1&
command2&
2. The ‘wait’ Command with the Ampersand ‘&’ Sign
The wait command waits for all child processes to exit. So using the wait command with the ‘&’ operator we can run batches of operations. However, the one drawback of using the wait command is that you can’t get a new process to start as soon as a running process ends.
The basic syntax >
command1&
command2&
wait
3. The ‘xargs’ Command
The xargs command can be used to execute commands in parallel by combining it with the ‘-p’ option. This option specifies the maximum number of parallel processes to run.
4. GNU Parallel
GNU Parallel is a powerful tool that allows you to run commands in parallel, providing flexibility and control over the execution process. You can install GNU Parallel on your system directly from the command line. By default, the parallel is not included & needs to be installed.
The basic syntax >
parallel ::: prog1 prog2
Here, ‘:::’ is a special syntax that tells the command to iterate over a list of arguments.
3 Practical Examples of Run Bash Commands in Parallel
In the following article, I will explain three practical examples of running Bash commands in parallel, based on the above-mentioned ways.
Example 1: Using the ‘&’ Sign with the ‘wait’ Command
In the first example, I will run a Bash script with multiple commands. Inside the script, I will use the “&” sign along with the wait command to perform parallel operations among those written Bash script commands.
➊ At first, launch an Ubuntu Terminal.
➋ Now, write the following command to open a script in the nano text editor:
nano example1.sh
❸ Afterward, write the following script in the nano editor:
#! /bin/bash
#Command 1 running in the background
ls -l&
#Command 2 running in the background
date&
#Command 3 running in the background
uname&
#wait for all background processes to complete
wait
echo “All commands completed"
Afterward, the ‘wait’ command waits for all the background processes to complete. When the processes are complete the ‘echo’ command prints the completion message.
➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
➎ After that, use the following command to make the script executable:
chmod u+x example1.sh
- chmod: Changes the permission of files and directories.
- u+x: Argument with chmod command to add the executable permission for the user.
- sh: File which you want to make executable.
After executing, you can see the script has executed all the commands parallelly & displaying the outputs one by one.
Example 2: Using GNU Parallel Command Tool
Here, I will use the powerful GNU command tool ‘parallel’ to run several Bash commands within a script in parallel. Now, this command tool is not installed by default. However, you can install it on your Ubuntu by running the following command:
sudo apt install parallel
Script (parallel.sh) > After that, the ’parallel’ command with the option ‘–jobs 3’ specifies that a maximum of 3 jobs should be executed concurrently. The ‘:::’ syntax followed by ‘“${commands[@]}”’ passes the elements of the ‘commands’ array as arguments to ‘parallel’.#! /bin/bash
commands=(
“ls -l”
“neofetch”
“curl”
)
parallel --jobs 3 ::: “${commannds[@]}”
echo “All commands completed"
The script prints all three commands’ output upon execution along with the ‘All commands completed’ message after all the operations are done.
Example 3: Run Commands in Parallel Using the xargs Command
In this example, I will use the xargs command in a Bash script to run some commands in parallel.
Script (xargs.sh) > The ‘printf “%s\n” “${commands [@]}”’ command prints each command output on a separate line. The output is then piped ( | ) to ‘xargs’, which reads the input lines & executes the command (‘sh -c “{}”) in parallel. And this command instructs the shell interpreter to execute the command represented by ‘{}’ will be replaced by each command from the array as ‘xargs’ processes them.#! /bin/bash
commands=(
“ls -l”
“echo “Hello, world!””
“uptime”
)
printf “%s\n” “${commands [@]}” | xargs -I {} -P 3 sh -c “{}”
echo “All commands completed"
The script prints all three commands’ output upon execution along with the ‘All commands completed’ message after all the operations are done.
Importance of Running Bash Commands in Parallel
Check out the significant outcomes you can get while running several Bash commands in parallel:
- By running commands in parallel, you can take the benefits of multicore processes and execute tasks concurrently. And that reduces overall execution time.
- When writing scripts or automation takes place, running commands in parallel within Bash scripts can increase efficiency ensuring faster execution.
- You can process a large number of files and even a complex command that takes a long time to execute.
- Improve the performance of a Bash script that is running on a single CPU.
Conclusion
In conclusion, learning to run several Bash commands in parallel can enhance your Bash scripting experience with speed, performance, & efficiency. Using some simple commands you can make it possible. I hope this article helps you learn the entire process.
People Also Ask
Related Articles
- How to make a File Executable in Bash [2 Methods]
- A Complete Overview of Bash Dot Command [With 2 Examples]
- What are the Usages of Bash Source [3 Practical Examples]
- How to Run a Bash Script [2 Methods with Cases]
<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial