this post was submitted on 23 Jun 2023
24 points (100.0% liked)

Linux

5237 readers
101 users here now

A community for everything relating to the linux operating system

Also check out [email protected]

Original icon base courtesy of [email protected] and The GIMP

founded 1 year ago
MODERATORS
 

cross-posted from: https://lemmy.run/post/15922

Running Commands in Parallel in Linux

In Linux, you can execute multiple commands simultaneously by running them in parallel. This can help improve the overall execution time and efficiency of your tasks. In this tutorial, we will explore different methods to run commands in parallel in a Linux environment.

Method 1: Using & (ampersand) symbol

The simplest way to run commands in parallel is by appending the & symbol at the end of each command. Here's how you can do it:

command_1 & command_2 & command_3 &

This syntax allows each command to run in the background, enabling parallel execution. The shell will immediately return the command prompt, and the commands will execute concurrently.

For example, to compress three different files in parallel using the gzip command:

gzip file1.txt & gzip file2.txt & gzip file3.txt &

Method 2: Using xargs with -P option

The xargs command is useful for building and executing commands from standard input. By utilizing its -P option, you can specify the maximum number of commands to run in parallel. Here's an example:

echo -e "command_1\ncommand_2\ncommand_3" | xargs -P 3 -I {} sh -c "{}" &

In this example, we use the echo command to generate a list of commands separated by newline characters. This list is then piped (|) to xargs, which executes each command in parallel. The -P 3 option indicates that a maximum of three commands should run concurrently. Adjust the number according to your requirements.

For instance, to run three different wget commands in parallel to download files:

echo -e "wget http://example.com/file1.txt\nwget http://example.com/file2.txt\nwget http://example.com/file3.txt" | xargs -P 3 -I {} sh -c "{}" &

Method 3: Using GNU Parallel

GNU Parallel is a powerful tool specifically designed to run jobs in parallel. It provides extensive features and flexibility. To use GNU Parallel, follow these steps:

  1. Install GNU Parallel if it's not already installed. You can typically find it in your Linux distribution's package manager.

  2. Create a file (e.g., commands.txt) and add one command per line:

    command_1
    command_2
    command_3
    
  3. Run the following command to execute the commands in parallel:

    parallel -j 3 < commands.txt
    

    The -j 3 option specifies the maximum number of parallel jobs to run. Adjust it according to your needs.

For example, if you have a file called urls.txt containing URLs and you want to download them in parallel using wget:

parallel -j 3 wget {} < urls.txt

GNU Parallel also offers numerous advanced options for complex parallel job management. Refer to its documentation for further information.

Conclusion

Running commands in parallel can significantly speed up your tasks by utilizing the available resources efficiently. In this tutorial, you've learned three methods for running commands in parallel in Linux:

  1. Using the & symbol to run commands in the background.
  2. Utilizing xargs with the -P option to define the maximum parallelism.
  3. Using GNU Parallel for advanced parallel job management.

Choose the method that best suits your requirements and optimize your workflow by executing commands concurrently.

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 1 year ago (2 children)

Missing details for & commands:

  • If you close the terminal, they'll be killed
  • You can prevent this by running as "nohup command &" which will also log their output in nohup.out
  • They're called background jobs and you can list them with the command jobs, bring one to the foreground with the command fg; crtl-z will freeze a foreground command and give you the prompt, and the command bg will background it again.
  • You can wait for all background jobs to finish by issuing the command "wait".
[–] ozr 2 points 1 year ago* (last edited 1 year ago)

Good post! It's also good to mention wait can be used with & (useful for scripts)

Example: command_1 & command_2 & command_3 & wait will make the shell wait until the last command finishes.

[–] [email protected] 1 points 1 year ago (1 children)

Can you background a command you started without the &?

[–] [email protected] 2 points 1 year ago

Yes, ctrl-z will background and freeze, giving you the prompt back; if you then issue "bg" it will unfreeze it in the background.

[–] [email protected] 3 points 1 year ago

Awsome, thanks! Should have thought of the ampersand myself...

[–] jiminside 1 points 1 year ago

I usually run something with tmux so I can detach the terminal