bg Command: Tutorial & Examples
Resume paused jobs and run them in the background
The bg
command stands for "background" and is used to resume paused or stopped jobs and run them in the background. It's a part of job control commands in a shell along with fg
, jobs
, and Ctrl+z
key combination.
Understanding how bg works
When you run a command or a process in the shell, that process is typically run in the foreground. This means you can't use the shell for other commands or tasks until this process is completed. With the bg
command, you can send this process to the background and free up your shell for other tasks.
For instance, if a process is paused or stopped using the Ctrl+z
key combination, you can use the bg
command to resume the process in the background. This process now runs independently of the shell, allowing you to continue using the shell for other tasks.
Why bg is important
The bg
command is crucial in multitasking in a Linux environment. It allows users to run time-consuming tasks without blocking the shell, thus optimizing the usage of system resources. This ability is especially useful when managing Linux servers, as it allows administrators to run multiple scripts or tasks concurrently.
How to use bg
Using the bg
command is straightforward. When a process is paused or stopped, you can use the bg
command followed by the job number to resume it in the background. Here's an example:
$ sleep 100
^Z
[1]+ Stopped sleep 100
$ bg %1
In this example, the sleep 100
command is paused using the Ctrl+z
key combination. The job number is 1
. The bg %1
command then resumes the job in the background.
Common command parameters
The bg
command doesn't have many parameters. The most common usage is bg %jobnumber
. Here, %jobnumber
is the job number that you want to resume in the background. You can get the job number from the output when you stop a job or by using the jobs
command.
Advanced usage scenarios
Consider the following scenarios where the bg
command can be particularly useful:
Running long scripts: If you're running a long-running script that you don't need to monitor, you can start it and send it to the background to free up the terminal for other commands.
Example:
$ ./long_script.sh ^Z [2]+ Stopped ./long_script.sh $ bg %2
Multiple background jobs: You can run multiple jobs in the background and check their status using the
jobs
command.Example:
$ sleep 200 & $ sleep 300 & $ jobs
Combining with other commands: You can combine
bg
withnohup
to run a command in the background that continues to run even after logging out.Example:
$ nohup long_running_task & $ bg %1
Potential problems and pitfalls with bg
command
While bg
command is generally safe to use, be aware that moving a process to the background could cause unexpected behavior if the process requires user interaction. Background processes are paused if they attempt to read input from the user.
Moreover, it's important to remember that if you close the parent shell, all background jobs will be terminated. To avoid this, use the nohup
command or tools like screen
or tmux
.
Common errors and troubleshooting
Here are some common errors you might encounter when using the bg
command:
Error: No job control: This error occurs if you attempt to use
bg
in a non-job control shell. Ensure you're using a proper shell that supports job control.Job stopped: If you see that a job is stopped and you cannot resume it, verify that it isn't waiting for user input. You may need to bring it to the foreground using the
fg
command.