The Linux terminal makes it unique and perfect for users who wish to operate their system using the commands. However, a few commands could take a lot of time, which can be frustrating because the terminal cannot do anything else, and you will have to wait until the terminal is free. To avoid this waiting, a Linux command can be run in the background. Keeping the importance in view, this post will demonstrate the possible methods to run Linux commands in the background.
The content of this post is as follows:
- How to Run Linux Commands in Background?
- Bonus Tip: How to Get Back Background Command on a Terminal?
Let’s discuss these methods in detail in the below section.
How to Run Linux Commands in Background?
As discussed earlier, running a Linux command is an essential task to come over the waiting of the terminal. This section has illustrated the use of various commands to run Linux commands in the background:
Using the Ampersand (&) Sign
Adding the ampersand symbol at the end of your command (after a single space from the last character) will immediately send it into the background giving the terminal’s control to the user.
For example, we can use the below command, which opens an untitled document that occupies the terminal, as seen below:
But when we use “&” at the end of our command, the terminal would be free for us to use:
$ gedit &
Using the ‘bg’ Command
So, you forgot to add “&” with your command and want to take it to the background? Don’t worry because Linux has a command that does the trick.
Before using this command, you must pause/stop the current process in the terminal by pressing “CTRL+ z” and executing this command:
When you execute the bg command, there will be a message on the terminal with a “&” at the end, indicating that the process will work in the background. We’d use this command to see the processes running in the background:
If you want to bring the commands running in the background to the front, use this command:
You can also kill the background process using its job ID, which you can see by using:
$ jobs -l
You can also kill the background process using its job ID, which you can see by using,
$ kill <job ID>
Using the nohup Command
No hang up no or nohup is best for users who want to keep their processes running even after the user exits the shell. A log file is automatically generated either in the current directory or $HOME.
The nohup command can be used as follows:
$ nohup sudo -n -PN -sT -sU -p- google.com
The log file can be accessed using this command:
$ nano nohup.out
Using the disown Command
The ampersand “&” sends the command (process) to the background right after it is executed, but when the shell is closed or the user logs out, the process is eliminated. We have the disown command to get rid of it.
It doesn’t work independently but requires the user to have at least one command running in the background, and how to do that is already discussed earlier.
So first, take the command (process) in the background like this.
*We are nmapping “google.com,” and you are to use your command*
Now that the command is run, let’s use this command to detach the process from the terminal.
Using tmux Utility
This is another utility that you can use to run Linux commands in the background, but for that, you need to install the tmux utility first by typing:
$ sudo apt install tmux
The above method is for Debian-based operating systems such as Ubuntu and Linux mint.
To install it on Fedora, you need to type:
$ sudo dnf install tmux
To install it on Archlinux you can type:
$ sudo pacman -S tmux
After that, you need to type the following command that will open the new terminal where you can run commands in the background:
Bonus Tip: How to Get Back Background Command on a Terminal?
If you want to go back to the main terminal, you can do that by typing the below command:
That’s all from this guide!
Playing with Linux commands can get you into trouble because all the hard work could be lost; some of them could take hours to complete. To eliminate this fuss, the user can run commands in the background. This post has provided a list of the possible methods to run a Linux command in the background. You have also learned about getting back the command from background to foreground.