Background and Foreground in Linux

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.

Background Processes

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:

  1. 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.

  2. Press Ctrl+Z to suspend the running process. Then, type bg to send it to the background and resume execution.

Do not log out of the server before saving your changes in the background because backgrounding a process does not survive a logout of your shell.

Foreground Processes

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.

Job Identification

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.

Hands-on Exercise Overview

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.

Managing Foreground Processes

  1. Start a command that runs indefinitely and suspend it with CTRL + Z:


    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.

  2. Run ping command to connect to every 5 seconds and terminate it with CTRL + C.

     ping -i 5

    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.

Managing Background Processes

  1. To start the previous ping command as a background process, append a & to the end of the command:

     ping -i 5 &

    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.

  2. To list all stopped or backgrounded processes, use the jobs command:


    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.

  3. To stop a specific background process use the kill command with the associated job number.

     kill %2

Moving Processes between Foreground and Background

  1. 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:


    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.

    Note that not all commands can be backgrounded. Some processes will automatically terminate if they detect that they have been started with their standard input and output directly connected to an active terminal.
  2. To move background processes to the foreground, type:


    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


  1. Linux Crash Course: Background (bg) and Foreground (fg)

  2. Processes

  3. How To Use Bash's Job Control to Manage Foreground and Background Processes