In Linux, most of the tasks are done through the terminals by executing the commands. Sometimes the execution of the commands takes a lot of time, and users have to wait for a while until the execution of the command is completed. To avoid this problem, Linux provides the facilities to run the commands in the background so that other operations can be performed simultaneously. Six ways can be considered to send a process in the background.
This post will demonstrate methods to send a Linux process to the background.
The content for the article is:
- Method 1: Send a Linux Process to the Background Using Ampersand (&)
- Method 2: Send a Linux Process to the Background Using bg
- Method 3: Send a Linux Process to the Background Using System Redirects
- Method 4: Send a Linux Process to the Background Using nohup
- Method 5: Send a Linux Process to the Background Using disown
- Method 6: Send a Linux Process to the Background Using tmux
Method 1: Send a Linux Process to the Background Using Ampersand (&)
The most convenient method to send a process to the background is using the Ampersand “&” at the end of the command. This will automatically assign the job id to the given tasks and run it in the background. Let’s check this method by executing it in the terminal.
To open the gedit text editor, we normally type “gedit” in the terminal, and the user can’t use the terminal until he closes the editor. But if we put an ampersand (&) at the end of the command, it will open the text editor in the background user can still use the shell for further executions:
$ gedit &
The gedit text editor is opened in the background.
Similarly, you can also use the wget command to download any file in the background:
$ wget https://download.teamviewer.com/download/linux/teamviewer_amd64.deb &
To view the background processes type the “jobs” command in the terminal:
The process is running in the background as shown in the image.
Let’s move toward method 2.
Method 2: Send a Linux Process to the Background Using bg
The second possible way to send a process to the background is by using the “bg” command after stopping the execution of the command. The execution of the command is stopped by pressing the “Ctrl+Z” and immediately typing “bg” in the terminal to send that process to the background. Let’s understand this concept in the below example.
We are downloading the “Nmap” tool in Linux. During download, the execution of the command is stopped by pressing “Ctrl+Z,” and instantly we typed “bg” in the terminal:
$ sudo apt install nmap
The “bg” command automatically puts ampersand (&) at the end of command, and the process will execute in the background.
Similarly, to open the firefox browser, we typed “firefox” in the terminal and which was stopped by pressing “Ctrl+z” and executed the “bg” command to send the process in the background.
To get back to the running tasks, the user can use the “fg” command with job number or job id:
$ fg %2
Note: simple “fg” commandreturns to the last running process.
Method 3: Send a Linux Process to the Background Using System Redirects
Another way to send any process to the background using the redirection operator. The below command will ping the given IP address and forward the output to the “Result.log” file, then “2” discard the error message (stderr), and finally “&1” is telling that destination is the file descriptor and forward the output to the file. The last “&” will run this command in the background and instantly give the shell control to the user:
$ ping -c3 192.168.1.1 >Result.log 2>&1 &
If we print the “Result.log” file, the output of the ping command will be stored in the file:
$ tail Result.log
The output of the file represents that 3 packets are transmitted.
Method 4: Send a Linux Process to the Background Using nohup
The nohup is a utility that runs the process or commands in the background even after logout. It will store the results of execution in the “nohup.out” file, which will be created automatically:
$ nohup sudo nmap -sS --top-ports=15 192.168.1.1/24
Let’s check the output of the “nohup.out” file using the cat command:
$ cat nohup.out
The result of the above command is stored in the file.
Method 5: Send a Linux Process to the Background Using disown
The disown utility, which keeps running the process or command after logging out or closing the terminal session. It will detach the process from the terminal and run it in the background. To use it, first, send the process to the background and then type disown command:
$ gedit & disown
The process runs in the background with job id 7862.
Method 6: Send a Linux Process to the Background Using tmux
The tmux is a utility to run multiple sessions of the terminal in a single window. It will make running commands in the background and will give access to the terminal for further execution of the commands:
$ tmux new -d 'ping -c3 220.127.116.11 > Result.log'
The execution of the above command will print the given IP Address in the background and print the output in the “Result .log” file.
Note: Users can also use the “>>” operator if the output file already exists.
Let’s print the “Result.log” file:
$ cat Result.log
The file output shows that 3 packets have been received from the given IP address.
That’s how you can send a process to the background.
In Linux, the process is sent to the background using the ampersand (&), bg command after stopping the process or using the nohup, disown, and tmux utilities. This write-up has briefly demonstrated all the possible methods to send a process to the background. The best method to send the process to the background is using the ampersand (&) at the end of the command.