fbpx bg Command in Linux: Background Processes (bg, fg, jobs) | Certificação Linux

Linux bg Command: Resume Stopped Background Processes

📅 01/01/2019 ⏱ 5 min ✍️ Uira Ribeiro
Linuxcommands
Linux bg Command: Resume Stopped Background Processes
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

As a multitasking operating system, Linux supports the execution of many processes - which 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 it is in the foreground. And if they don't need to be connected to the terminal to function, we say it is running in the background.

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

What are Foreground Processes?

A foreground process is any program that when executed, it is directly connected to the terminal. Some foreground processes allow 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 while awaiting and executing commands.

When you run a command in bash, a child process is created, and the terminal is handed over to the child process, which holds the terminal until it finishes executing.

In the example, when you type ls in bash, the ls command is executed in the foreground, and provides a list of files. It does not return the terminal to bash until it finishes listing the files:

$ ls

If there are many files in the directory, ls may take a while to return the command prompt to the user.

What are Background Processes?

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

The & symbol in Linux

Virtually any program that can be run without requiring user interaction can be 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 dumped to the terminal, even if that command is executing in the background. 

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

$ ls -l &[1] 14907[ec2-user@ip-172-30-0-241 etc]$ total 1444drwxr-xr-x  4 root  root       35 jun 21 18:54 acpi-rw-r--r--  1 root  root       16 mar  8  2019 adjtime-rw-r--r--  1 root  root     1518 jun  7  2013 aliases-rw-r--r--  1 root  root    12288 abr  5 13:44 aliases.dbdrwxr-xr-x  2 root  root      261 abr 18 20:16 alternativesdrwxr-xr-x  3 root  root       17 mar  8  2019 amazondrwxr-xr-x  2 root  root       50 abr 10 11:06 amplify-agen( ... )

Usually, programs that run in the background are prepared for this, so they don't send anything to standard output (STDOUT). But you can redirect the standard output of a command that was not prepared for this with the redirector:

$ ls -l > /tmp/saida &

Process Control

Each program that is put into execution in the shell is treated as a Job, which is nothing more than a Process that the shell manages.

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

  1. Foreground: 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 enter the symbol & at the end of a command line, the command is executed without occupying the terminal window. The shell prompt is displayed 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 be stopped. This job is called a stopped job.

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

The bg Command in Linux

After a process has started executing in the foreground, that is, connected to a terminal, it can be placed in the background.

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

The bg command in Linux places in the background a running process that was "frozen" by the SIGTSTP signal. See the example:

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

In this example the find utility is executed normally. During its execution the TSTP signal (ctrl-z) is sent and then it is placed in the background with the bg command and receives task number 1 by job control. 

Each process that is put into execution with "&" also receives a Job number by job control.

Bg also accepts the job number - 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.

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp