Linux Commands

How to Run Linux Commands in the Background

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:

$ [command] &

 
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.

$ sleep 40

 
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.

$ sleep 40 &

 

Now this process is in the background. To list the background running process use:

$ jobs -l

 

Now kill this background process using:

$ kill -9 [job-ID]

 
For example, to kill the current running sleep process use:

$ kill -9 6149

 
Now the sleep process is killed to confirm the list of the background running process using.

$ jobs -l

 

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.

$ gedit

 
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:

$ gedit &

 

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:

$ sleep 40

 

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:

$ bg

 
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:

$ jobs -l

 

To check the command running in background use:

$ fg

 

To kill the current process, use its job ID with kill command. To check jobs ID, run:

$ jobs -l

 

After knowing the jobs ID kill process using:

$ kill -9 [job-ID]

 
For example, to kill current running sleep process use:

$ kill -9 6584

 
To confirm whether the process is killed or not again run the jobs command:

$ jobs -l

 
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:

$ nohup ping google.com > 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:

$ cat GooglePing.txt

 

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.

$ ping google.com > GooglePing.txt &

 
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:

$ disown

 

Run the jobs command again to confirm the process is detached from terminal:

$ jobs -l

 
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:

$ cat GooglePing.txt

 

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:

$ sudo apt install tmux

 

To install on Fedora run:

$ sudo dnf install tmux

 
To install on Arch Linux run:

$ sudo pacman -S tmux

 
Now run the tmux command to open a new terminal:

$ tmux

 
To create a new tmux session use:

$ tmux new -s [session_id]

 
To list all tmux session use:

$ tmux ls

 
For killing a tmux session use command as:

$ tmux kill-session -t [session_id]

 
For example, to kill “0” tmux session use:

$ tmux kill-session -t 0

 
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:

$ exit

 

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.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.