Running Linux commands in the background allows the user to continue executing other tasks while the command is still running. To run a command in the background there are multiple ways; one of the simple methods is by adding an ampersand (&) at the end of the command line.
Once a process is set to run in the background, we can access it using the jobs command. This article features all different ways of running a command in the background.
List of different ways to run a background process is as follows:
Following is the detail of all methods to run a background process:
1: Using the Ampersand (&) Sign
In Linux Ampersand (&) is a shell operator that sends a command in the background as a separate process. By combining ampersand (&) to a command, it will be executed as a background process, allowing the shell to continue processing other commands immediately.
Syntax for a background process using Ampersand sign:
The ampersand symbol is always added at the end of command with a single space between “&” and last character of command.
For example, create a sleep process for 40 sec.
As we can see, the terminal is running the sleep command and not allowing us to perform any other task.
Now press “Ctrl + Z” to stop this process. Next, we will run the same sleep command this time but using an ampersand operator with it will set it up in the background.
Now this process is in the background. To list the background running process use:
Now kill this background process using:
For example, to kill the current running sleep process use:
Now the sleep process is killed to confirm the list of the background running process using.
Here is another example of using & ampersand sign is with gedit command.
For example, if we open the gedit text editor directly using the terminal we will be bound to use the shell for other work or else we need to stop the current process first.
Using the above command will open the text editor that will open it in front of the terminal.
But after using “&” at the end of gedit command, the shell is free to use for other processes:
2: Using the ‘bg’ Command
The bg command is the second way of running commands in the background. This command allows the user to continue working in the terminal while the process runs in the background, freeing up the terminal for other tasks. The bg command is used for a long running background process which even runs if the user logs out.
Like an ampersand sign we don’t have to worry about adding it to the command every time just send bg text to the terminal. It will start running the process in the background and free the terminal for future use.
For example, let’s use the previous example and send the command of sleep using:
Now the process is running and terminal is not available for use. To use the bg command and send this process in background we must first stop current executing process by pressing “Ctrl+ Z” and executing this command:
After sending the bg command the sleep process started continuing in the background. We can see an ampersand “&” at the end, indicating that the current process is in background running.
To check the background process run jobs command:
To check the command running in background use:
To kill the current process, use its job ID with kill command. To check jobs ID, run:
After knowing the jobs ID kill process using:
For example, to kill current running sleep process use:
To confirm whether the process is killed or not again run the jobs command:
We can see the sleep process is no longer running in background:
3: Using the nohup Command
The nohup command is third on our list of running background processes in Linux. The command stands for “no hang up” and is used to prevent the process from being terminated till the terminal session ends. When a process is executed using nohup, it continues to run even if the user logs out, and the output of the process is redirected to a file named “nohup.out”.
The nohup command to ping google continuously in background is and output is redirected to a text file GooglePing.txt:
After writing this command we can see the terminal is free and a background process for continuously pinging google is running in the background.
To access the log file, we can use the following command:
The log file can also be accessed using the GUI from the home screen.
4: Using the disown Command
The disown command removes a process from the shell’s and runs it in the background. When a process is disowned, it is no longer associated with the terminal and continues to run even after the user logs out or closes the terminal.
Previously we have used the ampersand “&” command that sends the process in background after its execution but as we close the shell the process gets terminated. To get rid of this disown command is there.
The disown command cannot work independently but a minimum of one process must be running in the background.
Run the below command with a & ampersand sign that will send your command to background.
After the file is created we can list it using the jobs command.
Now our process is running in background to detach it from terminal use:
Run the jobs command again to confirm the process is detached from terminal:
As our process is currently running but it is no longer shown inside the terminal:
To confirm our process, we can use the cat command to access that ping file:
We have successfully detached this process from the terminal but it is still running in the background.
5: Using the tmux Utility
Tmux is a kind of terminal multiplexer utility. Using tmux we can create multiple terminal sessions within a single shell. It provides the ability to run processes in the background.
To run a background process in Tmux, you can create a new session and then detach from it using the key combination Ctrl-b followed by d.
To install tmux utility on Ubuntu and Linux mint use:
To install on Fedora run:
To install on Arch Linux run:
Now run the tmux command to open a new terminal:
To create a new tmux session use:
To list all tmux session use:
For killing a tmux session use command as:
For example, to kill “0” tmux session use:
Here we listed the current running tmux active session and kill it using the above command:
-
- To detach from the tmux session press “Ctrl+B+D”.
- To get a list of all commands press “Ctrl+B+?”.
- To shift between tmux windows press “Ctrl+B+O”.
To split the tmux terminal vertically, press “Ctrl+B” and type %.
To get back on main terminal from tmux use:
Conclusion
Running background applications in Linux is a powerful feature that allows users to keep their long-running tasks running even after logging out of the terminal or closing the window. Usually an ampersand & sign or bg command is used to send a process in background. However, we can also use nohup or disown command to detach a process from the terminal. At last, we can use the tmux utility to create multiple terminals and run background processes without disturbing the terminal.