Linux fg Command

Linux 命令大全Complete List of Linux Commands

fgIt is an important command for process control in Linux/Unix systems, with the full name "foreground". Its main function is to switch jobs running in the background to the foreground to continue execution.

Why do you need the fg command

Imagine you are ordering at a restaurant:

  1. You order a main course (foreground process)
  2. At the same time, have the kitchen prepare dessert (background process)
  3. When you finish the main course, you can have the waiter bring the dessert to you (fg command)

In Linux, this foreground/background job switching mechanism allows you to:

  • Flexibly control the execution order of multiple tasks
  • Temporarily suspend the current task to handle more urgent matters
  • Efficiently utilize terminal sessions

Basic Syntax

fg [作业标识]

Parameter Description

Parameter Description
No parameter Resume the most recently backgrounded job
%n Resume the job with job number n
%str Resume the job starting with str
%?str Resume the job containing str
%+ or %% Equivalent to no parameter; resumes the most recent job
%- Resume the second most recently backgrounded job

Usage Examples

Example 1: Basic Usage

  1. Start a long-running task and put it in the background:

    sleep 60 &
    [1] 12345  # 系统返回作业号 [1] 和进程ID 12345
  2. View the current job list:

    jobs
    [1]+  运行中               sleep 60 &
  3. Bring the job back to the foreground:

    fg %1
    # 或者简写为
    fg

Example 2: Resume a Specific Job

Example

# Start multiple background jobs
python script1.py &
python script2.py &

# View the job list
jobs
[1]- Running python script1.py&
[2]+ Running python script2.py&

# Resume the first job
fg %1

How It Works

Process State Transition

Example

graph LR
A[Foreground running] -->|Ctrl+Z| B[Suspended]
    B -->|fg| A
B -->|bg| C[Background running]
    C -->|fg| A
  1. Foreground process: Occupies the terminal, receives keyboard input
  2. Background process: Does not occupy the terminal, cannot receive input
  3. Suspended process: Is suspended, waiting to be resumed

Signal Mechanism

When executingfg:

  1. The system sends to the jobSIGCONTsignal (continue execution)
  2. Set the job's process group as the terminal's foreground process group
  3. The terminal redirects input/output to the process

FAQ

Q1: How do I know which background jobs currently exist?

Usejobscommand to view:

Example

jobs -l

Example output:

[1]- 12345 运行中        sleep 100 &
[2]+ 12346 停止         vim file.txt

Q2: What is the difference between fg and bg?

Command Function Terminal control
fg Bring the job to the foreground Occupies the terminal
bg Let the suspended job continue running in the background Does not occupy the terminal

Q3: Why can't the program receive input after fg?

It may be that the program is suspended (displayed as "stopped"); you need to send a continue signal first:

Example

kill -CONT <PID>
fg %1

Practical Application Scenarios

Scenario 1: Development and Debugging

>

Start the development server:

Example

python manage.py runserver
# Press Ctrl+Z to suspend
[1]+ Stopped python manage.py runserver

Temporarily execute other commands

Resume the server:

Example

fg

Scenario 2: File Editing

Example

vim important_file.txt
# Press Ctrl+Z to suspend editing

# View file content
cat related_file.txt

# Return to editing
fg

Notes

  1. Terminal association: Background jobs are associated with the terminal that started them; closing the terminal will terminate the jobs
  2. Output interference: Background job output may suddenly appear in the foreground, interfering with current work
  3. Permission limitations: Some programs that require terminal interaction (such as password input) cannot run in the background
  4. Job number reuse: Job numbers are unique within a session, but different terminal sessions are counted independently

Advanced Tips

Using with nohup

Let the job run persistently in the background (even if you exit the terminal):

Example

nohup long_running_task &
# You can reconnect and resume later
fg %1

Using disown to Detach

Example

python server.py &
jobs -l               # Note down the job number
disown %1             # Detach from the terminal
# Now you can safely close the terminal

Linux 命令大全Complete List of Linux Commands

Other Extensions