LinuxCommandLibrary

fg

Move background process to foreground

TLDR

Bring most recently suspended or running background job to foreground

$ fg
copy

Bring a specific job to foreground
$ fg %[job_id]
copy

SYNOPSIS

fg [%job_id]

PARAMETERS

%job_id
    Specifies the job to bring to the foreground. Can be a job number (e.g., %1), a process ID (PID), or a job name prefix. If omitted, the most recently backgrounded or suspended job is brought to the foreground.

DESCRIPTION

The fg command in Linux is used to move a background job to the foreground. When a process is running in the foreground, it has direct access to the terminal's input and output. This means you can interact with the process, such as providing input or receiving output directly on the screen.
You can use fg after suspending a foreground process (using Ctrl+Z), or after explicitly starting a process in the background (using command &).
The command can be invoked in multiple ways; without arguments, it brings the most recently suspended or backgrounded job to the foreground. Alternatively, you can specify the job to bring to the foreground using its job ID (%job_id). This command is a useful tool for managing and interacting with multiple processes running from the same terminal session.

CAVEATS

If there are no suspended or background jobs, fg will report an error. The job ID must refer to an existing job.

JOB IDS

Job IDs are assigned sequentially as jobs are started. You can view active job IDs using the jobs command.

SIGNAL HANDLING

When a job is brought to the foreground, it receives signals (such as Ctrl+C to terminate) from the terminal. A background process typically needs to explicitly handle these signals, while a foreground process does so implicitly.

HISTORY

The fg command has been part of Unix-like operating systems since early implementations of job control in the 1970s. It was developed to provide a way to easily switch between multiple running processes in a single terminal, improving productivity and allowing users to manage several tasks simultaneously. Its usage has remained consistent over time, making it a fundamental tool for command-line interaction.

SEE ALSO

bg(1), jobs(1), kill(1), wait(1)

Copied to clipboard