Running Multiple Commands in the Background on Linux


Introduction

Executing multiple commands in the background is a useful feature in Linux that allows users to execute multiple tasks simultaneously. This can be particularly helpful when running long-running commands that may take a while to complete, as it allows the user to continue working on other tasks while the command is being executed in the background.

There are several ways to run commands in the background on Linux, including using the "&" operator and the "nohup" command. In this article, we will explore these methods and provide examples of how to use them.

Running Commands in Background Using "&" Operator

One of the most straightforward ways to run a command in the background on Linux is to use the "&" operator. This operator is used to run a command in the background and return control of the terminal to the user.

To use the "&" operator, simply append it to the end of the command that you want to run in the background. For example, to run the sleep command in the background, you would enter the following command

$ sleep 45 &

This command will execute the sleep command, which will cause the terminal to pause for 45 seconds, and then return control of the terminal to the user. The command will continue running in the background until it is completed.

You can use the jobs command to view a list of background jobs that are currently running on your system. For example −

$ jobs
[1]+ Running sleep 60 &

You can also use the "fg" command to bring a background job to the foreground and the "bg" command to send a job to the background. For example, to bring the "sleep" command to the foreground, you would enter the following command 

$ fg %1

Running Commands in the Background Using "nohup"

Another way to run a command in the background on Linux is to use the nohup command. This command is used to run a command that is immune to hangup signals, which allows the command to continue running even if the terminal is closed or the user logs out.

To use the nohup command, simply enter "nohup" followed by the command that you want to run in the background. For example, to run the sleep command in the background using nohup, you would enter the following command 

$ nohup sleep 60 &

This command will execute the sleep command and return control of the terminal to the user. The command will continue running in the background until it is completed, even if the terminal is closed or the user logs out.

By default, the output of the command will be redirected to a file called "nohup.out" in the current working directory. You can use the ">" operator to redirect the output to a different file if desired. For example 

$ nohup sleep 60 > output.txt &

This command will execute the "sleep" command and redirect the output to a file called "output.txt" in the current working directory.

Using "nohup" has the added benefit of allowing you to run a command in the background even if you are not logged into the system through a terminal. This can be useful if you want to run a command on a remote server and then disconnect from the server, for example.

Conclusion

Running multiple commands in the background on Linux is a useful feature that allows users to execute multiple tasks simultaneously. There are several ways to run commands in the background on Linux, including using the "&" operator and the "nohup" command. By using these methods, you can continue working on other tasks while long-running commands are being executed in the background.

Remember to use the "jobs" command to view a list of background jobs that are currently running on your system, and use the "fg" and "bg" commands to bring a background job to the foreground or send it to the background, respectively.

Using "nohup" has the added benefit of allowing you to run a command in the background even if you are not logged into the system through a terminal, which can be useful for running commands on remote servers.

Overall, running commands in the background is a valuable tool in Linux that can help increase productivity and efficiency. So, it is a very useful feature for Linux users.

Updated on: 04-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements