Processes and tasks

Command fg

Bring background task back to foreground

Linux · Bash examples · commands run on the selected server

When to use fg

Use fg to bring a shell-managed background or stopped job into the foreground. This reconnects terminal input to that job; it does not attach arbitrary processes created by another session.

Syntax

fg [%JOB]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

No arguments
Bring the current task back to the foreground.
%N
Select task by number jobs.

Usage examples

Run test task

sleep 120 &

A temporary background job is created in the current shell.

Check job number

jobs -l

Job numbers and process IDs are listed before selecting a job.

Bring the task back to the foreground

fg %1

The selected job takes foreground terminal control; the shell prompt returns when it stops or exits.

Notes and limitations

  • A shell builtin only affects its own jobs.
  • After returning to the foreground, Ctrl+C interrupts the job, Ctrl+Z stops it.

Common questions and troubleshooting

How do I stop a foreground command temporarily?

In an interactive shell with job control, Ctrl+Z normally stops it. Inspect jobs before resuming the intended task.

Can fg recover a task from a disconnected SSH session?

Not through a new shell's job table. Use a persistent terminal multiplexer or a managed service for work that must outlive a connection.

Further help

Options vary by version and distribution. Check the installed documentation with this command: help fg.