Background and Foreground in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The chage command, an abbreviation for “change age,” in Linux is used to view and modify user password expiry information. This command is particularly useful for system administrators who need to manage password policies across users in a Linux envi...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

In the context of Linux and Unix-like operating systems, foregrounding and backgrounding are concepts that relate to job control—a way of managing running processes from a shell (command line interface). When you run programs or commands in a shell, they can be executed in either the foreground or the background, affecting how they interact with the terminal and the user.
It is recommended not to open multiple terminals in one server. Instead, foreground and background processes should be used.
Running a process in the background means that a process is still running but doesn't have control over the terminal. It is associated with the specific terminal that started it but does not block access to the shell. Instead, it executes in the background, leaving the user able to interact with the system while the command runs. This is particularly useful for long-running tasks that don't require immediate user interaction. For example, a long-running task like a large file download can be initiated in the background, freeing up the terminal for other commands.
Because background processes return control to the shell immediately without waiting for the process to complete, many background processes can run at the same time.
There are two ways to send a process to the background:
Append & to the end of the command to send it to the background immediately. For example, cp largefile.txt backup & will copy the file while returning the terminal for other commands.
Press Ctrl+Z to suspend the running process. Then, type bg to send it to the background and resume execution.
By default, processes are started in the foreground. This means that until the program exits or changes state, it is not allowed to interact with the shell as it takes control of the terminal. This means that the user can interact with the process directly.
For example, if a text editor is run from the command line without backgrounding it, the terminal becomes occupied by the text editor, and it can't be used for other commands until the text editor is closed.
Because of the way that a foreground process interacts with its terminal, there can be only a single foreground process for every terminal window.
In a multitasking environment, multiple running processes in the background can be managed using job identifiers (job IDs) provided by the shell. The jobs command lists the running jobs and their statuses in the current shell session. A job can be a command or a pipeline of commands that are started as a single unit.

Here's a breakdown of the typical output format:
Job Number: Each job is assigned a unique job number (e.g., [1], [2], [3]) for identification. This number is used when you want to refer to a specific job using other job control commands like fg (foreground) and bg (background).
Current Job Indicators:
The + sign indicates the current job, which is the most recent job that was put into the background or stopped.
The - sign indicates the previous job, which is the second most recent job that was manipulated.
Jobs without these indicators are neither the current nor the previous job.
State: This shows the current state of the job. Common states include:
Running: The job is actively running in the background.
Stopped: The job has been suspended (e.g., by pressing Ctrl+Z). It is not currently running but can be resumed.
Done: The job has been completed. It means the command finished its execution.
Command Name: This is the command or pipeline that constitutes the job. It shows what command the job is running or was running.
This hands-on will focus on managing foreground and background processes and will demonstrate how to leverage your shell’s job control functions to gain more flexibility in how you run commands.
Start a command that runs indefinitely and suspend it with CTRL + Z:
htop
This utility will continue to run and update its display until it is terminated. Press CTRL + Z to suspend the running foreground process.

When pressing CTRL + Z, the terminal registers a “suspend” command, which then sends the SIGTSTP (signal terminal stop) signal to the foreground process. Essentially, this will pause the execution of the command and return control to the terminal.
Run ping command to connect to google.com every 5 seconds and terminate it with CTRL + C.
ping -i 5 google.com

Linux terminals are usually configured to send the “SIGINT” signal (short for “signal interrupt”) to the current foreground process when the user presses the CTRL + C key combination. The SIGINT signal tells the program that the user has requested termination using the keyboard.
To start the previous ping command as a background process, append a & to the end of the command:
ping -i 5 google.com &
This tells the shell not to wait for the process to complete, but instead to begin execution and immediately return the user to a prompt. The output of the command will still display in the terminal (unless redirected), but you can type additional commands as the background process continues.
The bash job control system will return output like this:

You’ll then receive the normal output from the ping command:

However, you can also type commands at the same time. The background process’s output will be mixed among the input and output of your foreground processes, but it will not interfere with the execution of the foreground processes.
To list all stopped or backgrounded processes, use the jobs command:
jobs
If the previous ping command is still running in the background, the jobs command’s output will be similar to this:

This indicates that you currently have two background processes, one is stopped and the second one is running. The [1], [2] represent the command’s job spec or job number. Reference this with other job and process control commands, like kill, fg, and bg by preceding the job number with a percentage sign. In this case, reference for instance, the second job as %2.
To stop a specific background process use the kill command with the associated job number.
kill %2

To move the currently running process to the background, first, the process must be stopped with CTRL+Z, and then bg command should be used to start it again in the background:
bg
The job status line will be shown again, this time with the ampersand appended:

By default, the bg command operates on the most recently-stopped process. Because background processes return control to the shell immediately without waiting for the process to complete, many background processes can run at the same time. If multiple processes have been stopped in a row without starting them again, you can reference a specific process by its job number to move the correct process to the background.
To move background processes to the foreground, type:
fg
This operates on your most recently backgrounded process (indicated by the + in the jobs command’s output). It immediately suspends the process and puts it into the foreground. To specify a different job, use its job number:
fg %2