Kiến Thức Linux

How to Run Linux Commands in Background

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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !