Skip to content

bg command on Linux (background processes)

As a multitasking operating system, Linux supports the execution of many processes - which they are nothing more than programs running. These programs can be run connected to the terminal or not.

If they are connected to the terminal, we say that it is in the foreground. And if they don’t need to be connected to the terminal to work, we say that it is running in the background.

The bg Command in Linux places a stopped process with the SIGSTOP signal in the background.

What are Foreground Processes?

A foreground process is any program that, when executed, is directly connected to the terminal. Some foreground processes allow for some type of interface that supports continuous user interaction, while others execute a task and “freeze” the terminal while the task is completed.

Bash itself is a process that is running in the foreground, waiting for user interaction when waiting and executing commands.

When you execute a command in bash, a child process is created, and the terminal is in charge of the child process, which holds the terminal until it finishes executing.

In the example, when you type ls in bash, the ls command runs in the foreground, and provides a list of files. It doesn’t return the terminal to bash until it finishes listing the files:

$ ls

If there are too many files in the directory, ls may be slow to return the command prompt to the user.

What are Background Processes?

A background process is any program that, when executed, is not connected to the terminal. It simply executes without any direct user interaction. These types of programs are generally daemons (services) that the system provides, such as a web server, email server, etc.

The & symbol in Linux

Virtually any program that can run without requiring user interaction can run in the background. To do this, simply add the “&” sign at the end of the command.

The output of the background command can still be downloaded to the terminal, even if that command is running in the background.

In the example, ls may be running in the background, but its output still goes to the terminal:

$ ls -l & <br></br>[1] 14907 <br></br>[ec2-user @ip -172-30-0-241 etc] $ total 1444 <br></br>drwxr-xr-x 4 root root 35 Jun 21 18:54 acpi <br></br>-rw-r--r-- 1 root 16 Mar 8 2019 adjtime <br></br>-rw-r--r-- 1 root ; root 1518 Jun 7 2013 aliases <br></br>-rw-r--r-- 1 root root 12288 Apr 5 13:44 aliases.db <br></br>drwxr-xr-x 2 root root 261 Apr 18 20:16 alternatives <br></br>drwxr-xr-x 3 root root 17 Mar 8 2019 amazon

drwxr-xr-x 2 root root 50 Apr 10 11:06 amplify-agen
(…)

Generally, programs that run in the background are prepared for this, so they don’t send anything for standard output (STDOUT). But you can send the standard output of a command that wasn’t prepared for this with driver:

$ ls -l > /tmp/output &

Process Control

Each program that is executed in the shell is treated as a Job (job), which is nothing more than a Process that the shell manages.

Each Job is assigned a sequential ID. Because a job is a process, each job also has an associated process ID (PID). There are three types of job statuses:

  1. Forefront: When you enter a command in a terminal window, the command occupies the terminal window until it is completed. This is a foreground job.
  2. Background: when you insert the & symbol at the end of a command line, the command is executed without occupying the terminal window. The shell prompt appears immediately after you press Enter. This is an example of a background job.
  3. Stopped: If you press Control + Z for a foreground job or enter the stop command for a background job, the job will stop. This job is called an interrupted job.

There are some commands that allow you to manage Jobs on Linux, such as bg, fg, and jobs.

The bg Command on Linux

Once a process has started running in the foreground, i.e. connected to a terminal, it can be placed in the background.

To do this, it is necessary to temporarily interrupt its execution with the SIGTSTP (20) signal by pressing the Ctrl-z keys and activating it immediately after the bg command.

The bg command on Linux puts in the background a running process that has been “frozen” by the SIGTSTP signal. See the example:

$ find/-name mss > lista_msg.txt Ctrl-z [1] + Stopped find/-name mss >list_msg.txt
$ bg [1] + find/-name mss >list_msg.txt &

In this example, the find utility runs normally. During its execution, the TSTP signal (ctrl-z) is sent and then placed in the background with the bg command and gets task number 1 through task control.

Each process that is executed with the “&” also earns a Job number for task control.

bg also accepts that job number - be passed as a parameter. To send the process back to the foreground (connected to the terminal), the fg command can be used.

And to check the list of background processes, the jobs command can be used.