YouTip LogoYouTip

Linux Comm Fg

[![Image 1: Linux Command Encyclopaedia](#) Linux Command Encyclopaedia](#) `fg` is an important command for process control in Linux/Unix systems, full name "foreground". Its main function is to switch a background job to the foreground to continue execution. ### Why the fg Command is Needed Imagine you are ordering food at a restaurant: 1. You ordered the main course (foreground process) 2. At the same time, you ask the kitchen to prepare dessert (background process) 3. When the main course is finished, you can ask the waiter to 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 use terminal sessions * * * ## Basic Syntax fg ### Parameter Description | Parameter | Description | | --- | --- | | No parameter | Resume the most recently backgrounded job | | %n | Resume job number n | | %str | Resume job starting with str | | %?str | Resume job containing str | | %+ or %% | Same as no parameter, resume 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 & 12345 # System returns job number and process ID 12345 2. View current job list: jobs + Running sleep 60 & 3. Bring the job to the foreground: fg %1# Or simply fg ### Example 2: Resume a Specific Job ## Example # Start multiple background jobs python script1.py & python script2.py & # View job list jobs - Running python script1.py & + Running python script2.py & # Resume the first job fg%1 * * * ## How It Works ### Process State Transition ## Example graph LR A -->|Ctrl+Z| B B -->|fg| A B -->|bg| C C -->|fg| A 1. **Foreground process**: Occupies the terminal, receives keyboard input 2. **Background process**: Does not occupy the terminal, cannot receive input 3. **Stopped process**: Suspended, waiting to be resumed ### Signal Mechanism When executing `fg`: 1. System sends `SIGCONT` signal to the job (continue execution) 2. Set the job's process group as the terminal's foreground process group 3. Terminal redirects input/output to that process * * * ## FAQ ### Q1: How to know what background jobs are currently running? Use `jobs` command to view: ## Example jobs-l Output example: - 12345 Running sleep 100 &+ 12346 Stopped vim file.txt ### Q2: What is the difference between fg and bg? | Command | Function | Terminal Control | | --- | --- | --- | | fg | Bring job to foreground | Occupies terminal | | bg | Let stopped job continue running in background | Does not occupy terminal | ### Q3: Why can't the program receive input after fg? The program might be stopped (shown as "Stopped"), you need to send the continue signal first: ## Example kill-CONT fg%1 * * * ## Practical Application Scenarios ### Scenario 1: Development Debugging > Start development server: ## Example python manage.py runserver # Press Ctrl+Z to stop + Stopped python manage.py runserver Temporarily execute other commands Resume server: ## Example fg ### Scenario 2: File Editing ## Example vim important_file.txt # Press Ctrl+Z to pause 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 job 2. **Output interference**: Background job output may suddenly appear in the foreground, interfering with current work 3. **Permission limitations**: Some programs requiring terminal interaction (like password input) cannot run in the background 4. **Job number reuse**: Job numbers are unique during a session, but different terminal sessions are counted independently * * * ## Advanced Tips ### Combine with nohup Let the job run persistently in the background (even after exiting terminal): ## Example nohup long_running_task & # Can reconnect later and resume fg%1 ### Use disown to detach ## Example python server.py & jobs-l# Note down the job number disown%1 # Detach from terminal # Now it's safe to close terminal [![Image 2:
← Linux Comm SourceLinux Comm Jobs β†’