fg Command: Tutorial & Examples

Bring a stopped or background job to the foreground in the terminal

The fg command is a fundamental tool in Unix/Linux job control that allows you to resume a stopped or background job by bringing it into the foreground of your terminal. This enables interactive control of processes that were previously suspended or running in the background, making it essential for managing multiple tasks in a single terminal session.

How Does It Work?

When you launch a command in a shell, it runs as a foreground job by default, meaning it receives input and output directly from your terminal. If you suspend this job using Ctrl+Z, the shell stops the process and places it in the background in a stopped state. Alternatively, you can start a job in the background by appending & to the command.

The fg command resumes a stopped or background job by moving it back to the foreground. This re-establishes the job's connection to your terminal's input and output, allowing you to interact with it directly.

What Is fg Used For?

fg is primarily used to:

  • Resume a job that was suspended with Ctrl+Z.
  • Bring a background job (started with &) back to the foreground.
  • Manage multiple jobs within a single terminal session by toggling which job is active.
  • Allow interactive control of processes without opening multiple terminals.

This is particularly helpful when you want to pause a long-running task temporarily, do something else, and then return to the task without killing or restarting it.

Why Is fg Important?

In Unix/Linux systems, multitasking is achieved by managing multiple jobs simultaneously. The fg command is a crucial part of job control, a feature that lets users start, stop, resume, and switch between processes in a terminal. Without fg, managing suspended or background jobs would be cumbersome, reducing productivity and control.

Using fg, system administrators and users can efficiently switch context without needing multiple terminal windows or sessions, making it an indispensable command for command-line multitasking.

How To Use fg

The basic syntax of fg is:

fg [%job_id]

If you have only one job suspended or running in the background, simply typing fg resumes that job in the foreground. If multiple jobs exist, you specify which one to bring forward by its job number, prefixed with %.

To see all current jobs and their states, use the jobs command.

Example 1: Bringing a Single Job To Foreground

$ sleep 300 &

[1] 12345

$ jobs

[1]+  Running                 sleep 300 &

$ fg

sleep 300

In this example, sleep 300 is started in the background. Using fg without arguments brings job [1] to the foreground.

Example 2: Multiple Jobs, Specifying Job Number

$ find / -name "myfile.txt" &

[1] 23456

$ ping google.com &

[2] 23457

$ jobs

[1]-  Running                 find / -name "myfile.txt &

[2]+  Running                 ping google.com &

$ fg %1

find / -name "myfile.txt"

Here, two jobs are running in the background. Using fg %1 brings the first job to the foreground.

Example 3: Resuming a Stopped Job

$ vi /etc/hosts

(Pressed Ctrl+Z)

[1]+  Stopped                 vi /etc/hosts

$ jobs

[1]+  Stopped                 vi /etc/hosts

$ fg

vi /etc/hosts

This example shows suspending an interactive editor with Ctrl+Z and resuming it with fg.

Common fg Command Parameters

fg itself does not have traditional options or flags. Instead, it accepts job specifiers as arguments:

  • %n — Refers to job number n. For example, %1 refers to job 1.
  • %string — Refers to a job whose command line begins with string.
  • %?string — Refers to a job whose command line contains string.

Most commonly, you use %n to specify the job number.

Common Errors and Troubleshooting

  • No such job: If you specify a job number that does not exist, you will see an error message like:

    fg: %3: no such job
    
  • Job requiring input appears hung: Bringing a job to the foreground that expects input may look like it hangs, but it is actually waiting for your input.

  • No current job: If you run fg without any jobs in the background or stopped, you will get:

    fg: current: no such job
    
  • Shell compatibility: Job control commands like fg depend on the shell. They work in interactive shells like bash or zsh, but may not behave as expected in scripts or non-interactive shells.

Tips and Best Practices

  • Use jobs frequently to keep track of suspended and background jobs.
  • Use Ctrl+Z to suspend a running foreground job before using fg to resume it later.
  • Combine fg with job specifiers to manage multiple jobs efficiently.
  • Avoid running critical tasks in the background if they require interaction; instead, keep them in the foreground or use terminal multiplexers like tmux.
  • Use bg to resume jobs in the background if you want them running but not occupying the terminal.

Comparison With Related Commands

  • bg: Sends a stopped job to the background and resumes it.
  • jobs: Lists current jobs with their status.
  • kill: Sends signals to jobs or processes, including to stop or terminate.
  • Ctrl+Z: Keyboard shortcut to suspend the current foreground job.

Whereas bg resumes a stopped job in the background, fg brings it to the foreground for interactive use.

Scripting and Automation

fg is intended for interactive use and does not work in non-interactive scripts because job control is generally disabled there. For scripting, consider managing processes with wait, kill, or running commands directly in the background with &.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA