Linux fg Command Guide: Resume Background Jobs in Terminal

Overview

When working inside a Linux shell, long-running commands often get pushed into the background or paused midway through execution. The fg (foreground) built-in command provides an effortless way to pull suspended or detached processes right back to your active interactive terminal session. This walkthrough explains how to combine fg alongside jobs, bg, and the Ctrl+Z suspension sequence to streamline shell-level multitasking and keep complete control over active workflows.

1. Spawning Detached Tasks and Inspecting Active Jobs

To establish an environment for testing shell job control, spawn a non-blocking dummy process that sleeps for 100 seconds in the background by appending an ampersand: sleep 100 &.

Spawning a background sleep task

Invoking jobs -l yields a detailed inventory of every background and suspended process running under the active shell session, explicitly exposing their associated Process IDs (PIDs).
Having this granular view makes it trivial to distinguish individual Job IDs when mapping out targets for foreground restoration.
⚙️ [Essential Command Flags]

-l : Appends the underlying Process ID (PID) alongside standard job sequence numbers and operational statuses.
-p : Emits strictly the numerical Process IDs (PIDs) while stripping all supplemental contextual details.
-n : Filters output exclusively for tasks whose states underwent changes since the shell’s most recent status notification.
-r : Restricts reporting specifically to tasks currently marked in an active Running state in the background.
-s : Limits matches strictly to tasks that have been Suspended or Stopped.

Inspecting jobs with process identifiers

2. Basic fg Usage: Restoring the Default Task

Launch another ephemeral timer set to pause for 20 seconds as a detached task via sleep 20 & to explore default recovery behavior.

Launching a secondary background worker

Query the shell’s process pool by running a bare jobs invocation to detect which command is queued as the default candidate.
The item marked with a + symbol in the resulting list represents the default target—the task most recently pushed into the background or stopped, which acts as the target for bare job commands.

Viewing the active jobs pool

Entering fg without extra parameters instantly restores this default background process to your active shell interface.
The task immediately reclaims standard input/output streams and retains control over your terminal until its execution completes or gets interrupted.

Restoring the default process with fg

3. Targeted Control: Pulling Specific Tasks by Job ID

To demonstrate fine-grained selection across multiple background entries, spawn a task scheduled to run for 150 seconds using sleep 150 &.

Adding a 150-second background process

Immediately queue another concurrent worker by triggering a 200-second timer using sleep 200 &.

Adding a 200-second background process

Evaluate your current process table by issuing jobs to inspect the newly assigned indices.
The bracketed integers on each line correspond to the individual Job IDs assigned sequentially by the current shell environment.

Listing multiple concurrent background jobs

Executing fg %1 specifically addresses and pulls the process assigned to Job ID 1 back into the active foreground.
Prefixing the intended index number with the % token allows you to address any targeted process without relying on default selection heuristics.

Bringing specific job 1 to foreground

4. Chaining Workflow Controls: Ctrl+Z, bg, and fg

To inspect state manipulation within a complete lifecycle pipeline, initiate an extended 300-second job using sleep 300 &.

Initiating extended 300-second task

Run jobs -s to isolate and verify processes in your session that reside exclusively in a Stopped or suspended state.

Filtering for stopped processes

Issue bg %1 to instruct the halted task under Job ID 1 to resume running silently in the background rather than staying frozen.

Resuming execution in background with bg

Confirm that the target has transitioned successfully into an active execution cycle by listing running processes via jobs -r.

Listing active running jobs

Finally, call fg %1 to intercept the running background thread, reattaching terminal input and pulling it back onto your interactive console.

Reconnecting terminal control with fg

We have explored the fundamental mechanisms and daily workflows of the Linux fg utility alongside standard job control operators.
Mastering these terminal management commands will significantly accelerate your productivity when juggling simultaneous development or administration tasks.