Linux fg Command
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:
- You order a main course (foreground process)
- At the same time, have the kitchen prepare dessert (background process)
- 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
Start a long-running task and put it in the background:
sleep 60 & [1] 12345 # 系统返回作业号 [1] 和进程ID 12345
View the current job list:
jobs [1]+ 运行中 sleep 60 &
Bring the job back to the foreground:
fg %1 # 或者简写为 fg
Example 2: Resume a Specific Job
Example
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
A[Foreground running] -->|Ctrl+Z| B[Suspended]
B -->|fg| A
B -->|bg| C[Background running]
C -->|fg| A
- Foreground process: Occupies the terminal, receives keyboard input
- Background process: Does not occupy the terminal, cannot receive input
- Suspended process: Is suspended, waiting to be resumed
Signal Mechanism
When executingfg:
- The system sends to the job
SIGCONTsignal (continue execution) - Set the job's process group as the terminal's foreground process group
- The terminal redirects input/output to the process
FAQ
Q1: How do I know which background jobs currently exist?
Usejobscommand to view:
Example
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
fg %1
Practical Application Scenarios
Scenario 1: Development and Debugging
>Start the development server:
Example
# Press Ctrl+Z to suspend
[1]+ Stopped python manage.py runserver
Temporarily execute other commands
Resume the server:
Example
Scenario 2: File Editing
Example
# Press Ctrl+Z to suspend editing
# View file content
cat related_file.txt
# Return to editing
fg
Notes
- Terminal association: Background jobs are associated with the terminal that started them; closing the terminal will terminate the jobs
- Output interference: Background job output may suddenly appear in the foreground, interfering with current work
- Permission limitations: Some programs that require terminal interaction (such as password input) cannot run in the background
- 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
# You can reconnect and resume later
fg %1
Using disown to Detach
Example
jobs -l # Note down the job number
disown %1 # Detach from the terminal
# Now you can safely close the terminal