YouTip LogoYouTip

Linux Comm Bg

# Linux bg Command [![Image 4: Linux Command Encyclopedia](#) Linux Command Encyclopedia](#) `bg` is one of the core commands in Linux/Unix systems for job control, standing for "background." Its main function is to move a currently paused job to run in the background. ### Basic Concepts * **Foreground Job**: A process running in the terminal and occupying input/output. * **Background Job**: A process running in the background without occupying input/output. * **Job Control**: The ability to manage foreground and background processes. * * * ## Command Syntax bg ### Parameter Description | Parameter | Description | | --- | --- | | No Parameters | Operates on the current job (the most recently paused job) | | Job Number | Specifies the job number to operate on (can be viewed using the `jobs` command) | * * * ## Usage Scenarios ### 1. Resume a Paused Job After pausing a foreground job with `Ctrl+Z`, you can use the `bg` command to continue it in the background: ## Example $ sleep 100# Start a long-running command ^Z # Press Ctrl+Z to pause + Stopped sleep 100 $ bg# Let it continue running in the background + sleep 100& ### 2. Resume by Specifying a Job Number When there are multiple paused jobs, you can specify the job number: ## Example $ jobs - Stopped vim file1.txt + Stopped python script.py $ bg 2# Let the second job continue running in the background ### 3. Used in Combination with the fg Command `bg` and `fg` (foreground) are complementary commands: * `bg`: Moves a job to run in the background * `fg`: Brings a job back to the foreground * * * ## Practical Application Examples ### Scenario: Typical Workflow in Development ## Example # 1. Start an Editor $ vim app.py # 2. Need to temporarily run tests (press Ctrl+Z to pause vim) ^Z + Stopped vim app.py # 3. Run Test Script $ python test.py # 4. After testing, let the editor return to the background to continue working $ bg 1 + vim app.py & # 5. Check Background Jobs $ jobs + Running vim app.py & * * * ## FAQs ### Q1: How do I check which jobs are currently running? Use the `jobs` command: ## Example $ jobs - Running sleep 100& + Stopped vim notes.txt ### Q2: What's the difference between `bg` and `&`? * `&`: Directly runs the command in the background when started. * `bg`: Moves an already paused job to the background. ### Q3: What happens to a `bg` job after closing the terminal? By default, background jobs will terminate when the terminal closes. To keep them running, use: ## Example nohup command&# or use tmux/screen * * * ## Advanced Tips ### 1. Use with disown Unbind a job from the terminal so it won't terminate even if the terminal is closed: ## Example $ sleep 1000 ^Z $ bg $ disown%1 ### 2. Use Job Number Shortcuts * `%n`: Job number n * `%str`: Jobs starting with str * `%?str`: Jobs containing str ## Example $ bg%sleep# Resume jobs containing "sleep" * * * ## Summary Flowchart !(#) Mastering the `bg` command can significantly improve your efficiency in Linux, especially when handling multiple tasks simultaneously. Combined with commands like `jobs`, `fg`, and `kill`, you can achieve powerful job control capabilities. [![Image 6: Linux Command Encyclopedia](#) Linux Command Encyclopedia](#)
← Linux Comm EnvLinux Comm History β†’