Linux fg Command
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:
- You order the main course (foreground process)
- and at the same time ask the kitchen to prepare dessert (background process)
- 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:
- flexibly control the execution order of multiple tasks
- temporarily suspend the current task to handle something more urgent
- make efficient use of the terminal session
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
-
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
]
- Running python script1.py
&
[
2
]
- Running python script2.py
&
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
- Foreground process: occupies the terminal and receives keyboard input
- Background process: does not occupy the terminal and cannot receive input
- Suspended process: halted and waiting to be resumed
Signal mechanism
When executing fg When:
- the system sends the job a
SIGCONTsignal (continue execution) - Set the job's process group as the terminal's foreground process group
- 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
%
1Start the development server:
Example
python manage.py runserver
Press Ctrl+Z to suspend
[
1
]
- Stopped python manage.py runserver
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
- Terminal association: a background job is tied to the terminal that started it; closing the terminal terminates the job
- Output interference: a background job's output may suddenly appear in the foreground, disrupting current work
- Permission restrictions: programs that need terminal interaction (such as password entry) cannot run in the background
- 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
Linux Command Reference