Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux fg Command

Linux fg Command

Linux Command Reference Linux Command Reference

fg is an important command for process control in Linux/Unix systems, short for "foreground". Its main function is to bring a job running in the background to the foreground to continue executing.

Why the fg command is needed

Imagine you are ordering food at a restaurant:

  1. You order the main course (foreground process)
  2. and at the same time ask the kitchen to prepare dessert (background process)
  3. After you finish the main course, you can ask the waiter to bring the dessert to you (the fg command)

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


Basic Syntax



fg [作业标识]

Parameter Description

Parameters Description
No parameters Resume the most recently backgrounded job
%n Resume the job with job number n
%str Resume the job whose name starts with str
%?str Resume the job whose name contains str
%+ or %% equivalent to no arguments; resumes the most recent job
%- Resume the second-to-last backgrounded job

Usage examples

Example 1: Basic Usage

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

sleep 60 & [1] 12345 # The system returns job number [1] and process ID 12345 2.

View the current job list:

jobs

[1]+  运行中               sleep 60 &
3.

Bring a 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

]

&

[

2

]

&

Resume the first job

fg

%

1


How It Works

Process state transitions

Example

graph LR

    A[Foreground running] -->|Ctrl+Z| B[Suspended state]

    B -->|fg| A

    B -->|bg| C[Background running]

    C -->|fg| A

  1. Foreground process: occupies the terminal and receives keyboard input
  2. Background process: does not occupy the terminal and cannot receive input
  3. Suspended process: halted and waiting to be resumed

Signal mechanism

When executing fg When:

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

Frequently Asked Questions

Q1: How can I tell which background jobs are currently running?

Use jobs Check with the command:

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 a job to the foreground occupies the terminal
bg Let a suspended job continue running in the background does not occupy the terminal

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

The program may be suspended (shown as "Stopped"); send a continue signal first:

Example

kill

-CONT

<

PID

fg

%

1

Start the development server:

Example

python manage.py runserver

Press Ctrl+Z to suspend

[

1

]

Temporarily run other commands

Resume the server:

Example

fg

Scenario 2: File editing

Example

vim

important_file.txt

Press Ctrl+Z to suspend the editor

View the file contents

cat

related_file.txt

Return to editing

fg


Notes

  1. Terminal association: a background job is tied to the terminal that started it; closing the terminal terminates the job
  2. Output interference: a background job's output may suddenly appear in the foreground, disrupting current work
  3. Permission restrictions: programs that need terminal interaction (such as password entry) cannot run in the background
  4. Job number reuse: job numbers are unique within a session, but different terminal sessions count independently

Advanced Tips

Using with nohup

Keep jobs running persistently in the background (even after exiting the terminal):

Example

nohup

long_running_task

&

You can reconnect and resume later

fg

%

1

Use disown to disassociate

Example

python server.py

&

jobs

-l

             

Note the job number

disown

%

1            

Remove the association with the terminal

Now you can safely close the terminal

Linux Command Reference Linux Command Reference