[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 13, 2023

How to Run Linux Commands in Background

How To Run Linux Commands In Background.

In this article, we will talk about the background processes is Linux. We will show you how to start a command in the background and how to keep the process running after the shell session is closed.

In this article, we will talk about the background processes is Linux. We will show you how to start a command in the background and how to keep the process running after the shell session is closed.

Typically when you run a command in the terminal, you have to wait until the command finishes before you can enter another one. This is called running the command in the foreground or foreground process.

 

Run a Linux Command in the Background

To run a command in the background, add the ampersand symbol (&) at the end of the command:

command &

The shell job ID (surrounded with brackets) and process ID will be printed on the terminal:

[1] 23088

You can have multiple processes running in the background at the same time.

The background process will continue to write messages to the terminal from which you invoked the command. To suppress the stdout and stderr messages use the following syntax:

command > /dev/null 2>&1 & 

>/dev/null 2>&1 means redirect stdout to /dev/null and stderr to stdout .

Use the jobs utility to display the status of all stopped and background jobs in the current shell session:

jobs -l

The output includes the job number, process ID, job state, and the command that started the job:

[1]+ 25177 Running                 ping google.com &

To bring a background process to the foreground, use the fg command:

fg

If you have multiple background jobs, include % and the job ID after the command:

fg %1

To terminate the background process, use the kill command followed by the process ID:

kill -9 15775

 

Move a Foreground Process to Background

To move a running foreground process in the background:

  1. Stop the process by typing Ctrl+Z.

  2. Move the stopped process to the background by typing bg.

 

Keep Background Processes Running After a Shell Exits

If your connection drops or you log out of the shell session, the background processes are terminated. There are several ways to keep the process running after the interactive shell session ends.

One way is to remove the job from the shell’s job control using the disown shell builtin:

disown

If you have more than one background jobs, include % and the job ID after the command:

disown %1

Confirm that the job is removed from the table of active jobs using the jobs -l command. To list all running processes, including the disowned use the ps aux command.

Another way to keep a process running after the shell exit is to use nohup.

The nohup command executes another program specified as its argument and ignores all SIGHUP (hangup) signals. SIGHUP is a signal that is sent to a process when its controlling terminal is closed.

To run a command in the background using the nohup command, type:

nohup command &

The command output is redirected to the nohup.out file.

nohup: ignoring input and appending output to 'nohup.out'

If you log out or close the terminal, the process is not terminated.

 

Alternatives

There are a number of programs that allow you to have multiple interactive sessions at the same time.

 

Screen

Screen or GNU Screen is a terminal multiplexer program that allows you to start a screen session and open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.

 

Tmux

Tmux is a modern alternative to GNU screen. With Tmux, you can also create a session and open multiple windows inside that session. Tmux sessions are persistent, which means that programs running in Tmux continue to run even if you close the terminal.

 

Conclusion

To run a command in the background, include & at the end of the command.

When you run a command in the background, you don’t have to wait until it finishes before you can execute another one.

Keep reading
How to use uniq command in Linux.
October 10, 2023

How to use uniq command in Linux.

How To Use Uniq Command In Linux. Linux command-line tools offer a wide range of functionality to help users manage and manipulate text data efficiently. One such utility is the uniq command, which is used for identifying and removing duplicate lines from text files. In this article, we will…

Read article
How to use tput command in Linux.
October 10, 2023

How to use tput command in Linux.

How To Use Tput Command In Linux. The tput command is a versatile and powerful tool in the Linux command-line arsenal. It is primarily used for manipulating and querying terminal capabilities, such as cursor control, text formatting, and color manipulation. In this article, we will explore the…

Read article
How to use split command in Linux.
October 10, 2023

How to use split command in Linux.

How To Use Split Command In Linux. In this guide, learn how to use the Linux split command with examples. The Linux split command breaks files into smaller parts and is used for analyzing large text files with many lines. While each split file counts 1000 lines by default, the size is changeable.…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00