Premium lesson

Managing Foreground & Background Processes

Shell·3 min read·Jan 1, 2025

By default, when executing a command, the resulting process created and executed by the shell runs in the foreground of the terminal, thus blocking its usage until it completes.

While most processes are short-lived and complete their execution in a matter of milliseconds or a few seconds, this behaviour is not desirable for longer-lived processes, such as web servers or processing scripts.

Running a process in the background

To run a process in the background, you can append an ampersand character & to your command as follows:

$ command &

This will cause the shell to execute the resulting process of that command as a background task called a job and immediately give you the command prompt back to perform other tasks.

Additionally, the shell will output the job number and process identifier of the task in the following format:

[job_number] process_identifier

Where:

  • The job number is an integer assigned by the job manager to identify a running or suspended background process.

  • The process identifier is an integer assigned by the system itself to uniquely identify an active process.

icon light bulb key

Unlock the CLI & Scripting with Bash module

Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.

You get immediate access to:

  • 34 focused lessons across the CLI and Bash
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module