Overview
`bg` stands for 'background' and is a command used to move a suspended (Stopped) job to the background and resume its execution (Running) after pressing `Ctrl+Z`. It's useful when you need to stop a foreground job and perform other tasks, allowing the original job to continue without closing the terminal session. It can be used with a job number, which can be identified using the `jobs` command, to specify a particular job.
Key Features
The main features of the `bg` command are as follows:
- Resumes suspended jobs in the background.
- Allows specifying individual jobs via their job number.
- Enables long-running tasks to continue without relinquishing terminal control.
Difference between bg and fg
Both `bg` and `fg` control background jobs, but their roles differ.
- bg: Switches a suspended job to continue running in the background.
- fg: Switches a job running in the background to the foreground, taking terminal control.
Key Options
The `bg` command is primarily used with arguments (job numbers). There are no complex separate options.
1) Execution Options
Generated command:
Try combining the commands.
Description:
`bg` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore the functionality of the `bg` command through various usage examples.
Suspend a job and then move it to the background
ping google.com
(Ctrl+Z 입력)
jobs
bg
Run the `ping` command, suspend it with `Ctrl+Z`, verify with `jobs`, and then resume it in the background with `bg`.
Resume multiple suspended jobs in the background
sleep 100
(Ctrl+Z 입력)
vi /etc/hosts
(Ctrl+Z 입력)
jobs
bg %1
bg %2
Suspend two jobs, verify with `jobs`, and then resume both in the background with `bg`.
Resume a specific suspended job in the background
jobs
bg %2
Move the second job (`%2`) from the list (identified by `jobs`) to the background.
Installation
`bg` is a built-in command of shells (like bash, zsh, etc.) and does not require separate installation.
Tips & Considerations
Here are some points to consider when using the `bg` command.
Tips
- Suspending a job with `Ctrl+Z` does not terminate it. It remains in memory, so you should use commands like `jobs`, `bg`, `fg`, or `kill` to manage it as needed.
- While `bg` can restart a job in the background, jobs that send output to the terminal may continue to display output even in the background. You can hide the output using redirection like `> /dev/null 2>&1`.
- If no arguments are specified when running `bg`, it automatically selects the most recently suspended job.