How to Run Linux Commands in Background

July 5, 2023

Introduction

Running Linux commands in the background allows users to execute a command without waiting for the process to complete. This method lets users initiate processes and continue working on other tasks simultaneously.

This guide explores different methods of running Linux commands in the background.

How to Run Linux Commands in Background

How to Run Linux Commands in Background

When working in the terminal, commands are executed in the foreground, requiring users to wait for them to finish before entering another command.

However, an alternative option is to run a command in the background. The method allows the command to run without user input or interaction, which is convenient for long-running processes.

The following text elaborates on several methods for running commands in the background.

Using Ampersand 

One way to run a command in the background is by appending an ampersand (&) at the end.

The ampersand symbol instructs the shell to execute the command as a separate background process. To run a background process, use the following:

[command] &

For example, the Vim text editor starts as the foreground process, and the shell becomes unusable until the user closes the editor. However, by adding an ampersand to the command, users are able to use the shell immediately. To run Vim in the background, execute:

vim &
vim & terminal output

The terminal displays:

  • [1] - The shell job ID.
  • 2781 - The process ID of the background command.

Check the background processes' status in the current shell session with the jobs command:

jobs
jobs Vim terminal output

To bring a background process back to the foreground, use fg followed by the job ID or process ID.

For instance, bring Vim back to the foreground using the job ID with:

fg 1%

To use the process ID, execute:

fg 2781
Vim opened

Using bg

Unlike appending an ampersand, bg moves a stopped process from the foreground to the background. This is useful when a process is already in execution and needs to be continued in the background.

For instance, sudo apt update is already running:

sudo apt update terminal output

Hit Ctrl + z to stop it:

sudo apt update stopped with ctrl+z terminal output

The last output line indicates that the process has been stopped. To continue running sudo apt update in the background, run:

bg
bg terminal output

The output indicates that the process runs in the background.

Running jobs without any arguments also confirms that the process is running in the background:

jobs terminal output

Using nohup

The nohup utility makes a command immune to hangup signals. A command executed with nohup continues running even if the terminal session ends or the user logs out. The output is redirected to a log file (nohup.out) by default.

The nohup syntax is:

nohup [command] &

For example, to ping a website in the background, run:

nohup ping phoenixnap.com &
nohup ping ampersand terminal output

Even after the terminal session ends, the command continues working. Return to the terminal and run jobs to verify:

jobs
jobs nohup terminal output

The output saves in the nohup.out log file. Verify the log with cat:

cat nohup.out
cat nohup.out terminal output

Using System Redirects

Send a command to the background with system redirects. This utility allows users to redirect the standard output and standard error streams to a specific log file. The command detaches from the terminal and discards the output, leaving the terminal free.

The syntax is:

[command] output.log>&1 &

The syntax consists of:

  • The command.
  • output.log - The log file to which the system redirects the output.
  • 2>&1 - Redirects errors to the same log file.
  • & - Instructs the system to run the command in the background.

For example, ping the same website using the system redirect:

ping phoenixnap.com >output.log 2>&1 &
system redirect ping website output.log terminal output

The terminal is usable right away. Verify that the command still runs in the background with jobs:

the jobs command output for system redirect

The ping output is redirected to the output.log file. Confirm this with:

cat output.log
cat output.log terminal output

To disregard the output, replace the log file with /dev/null:

ping phoenixnap.com >/dev/null 2>&1 &
system redirect ping website /dev/null terminal output

Using disown

The disown command removes a process from the shell. However, the process keeps running in the background. With disown, users can close the terminal without affecting the command in the background.

For instance, run sudo apt update in the background using the ampersand:

sudo apt update &
sudo apt update ampersand terminal output

Run disown to detach the process from the shell:

disown
disown sudo apt update terminal output

To verify that the process is not part of the shell, run jobs:

verify disown command with jobs

The lack of output shows that the process is no longer affected by the shell. Confirm that the process is running outside the shell with the following command:

ps aux | grep [process ID]

The ps command lists active processes, and the grep argument searches for the specific process ID and shows relevant details. The process ID for the command in question is 2401. Therefore, run:

ps aux | grep 2401
pas aux grep terminal output

The output confirms that sudo apt update is still running.

Using Tmux

Another way to run commands in the background is with Tmux, a terminal multiplexer utility. The tool allows users to manage multiple terminal sessions within a single shell.

Users can create new windows, split panes, and switch between them, which enables efficient multitasking and background command execution.

Note: Tmux doesn't come preinstalled in Linux. To install it, run sudo apt install tmux.

Once installed, launch Tmux by running:

tmux
Tmux session

The command opens a new Tmux session.

After opening a new window in Tmux, split the window vertically to create two panes using Ctrl+b followed by the % key.

Tmux two panes

To navigate between panes, use the Ctrl+b, followed by the Right or Left Arrow key.

Note: Depending on the system, the panes might be one above the other. In that case, navigate between the panes using the Ctrl+b and the Up or Down Arrow keys.

The current setup allows users to run different commands in different panes and to return to the main terminal window.

The following example shows one pane running sudo apt update and the other a text opened in the Vim text editor:

Tmux different commands in split panes

To go back to the original terminal window, press Ctrl+b, followed by the d key:

Tmux detached from the session

The key combination detaches the Tmux session and returns to the original terminal. However, the session continues running in the background. To return to the session, enter:

tmux attach

Conclusion

After reading this tutorial, you know how to run Linux commands in the background. Use the tool that suits you best and follow the instructions from this guide.

Next, read this ultimate list of Linux commands all users should know.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
Bash wait Command with Examples
September 23, 2021

The wait command helps control the execution of background processes. Learn how to use the wait command through hands-on examples in...
Read more
How to Use the top Command in Linux
December 2, 2021

The top command is a built-in Linux utility that contains useful information about your system's running processes and resource usage. Learn how to use the top command effectively and explore its...
Read more
How to Kill a Process in Linux? Commands to Terminate
December 13, 2023

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it. Most processes have their own methods of shutting down...
Read more
How to Use the Linux sleep Command with Examples
February 3, 2021

The sleep command is used when it is necessary to postpone the execution of commands on the command line...
Read more