2024-08-24
bg
CommandThe bg
command, short for “background,” is used to resume a stopped job and run it in the background. A “job” in this context refers to a process that’s been suspended, typically using Ctrl+Z. It’s essential to remember that bg
only works on jobs that are already suspended. Simply running a command and then trying to use bg
will not work.
Key Features:
bg
takes a suspended job and restarts it, allowing you to continue working in your terminal while the job completes.fg
(foreground), jobs
, and kill
.bg
in ActionLet’s illustrate bg
’s functionality with practical examples. Assume you’re running a long-running process, such as a large file download or a computationally intensive script.
Example 1: Suspending and Backgrounding a sleep
command
$ sleep 60 # Starts a process that sleeps for 60 seconds
^Z # Press Ctrl+Z to suspend the process
$ jobs # List current jobs
[1]+ Stopped sleep 60
$ bg %1 # Resume job 1 in the background. %1 refers to job number 1.
[1]+ sleep 60 &
$ echo "The sleep command is now running in the background."
The sleep command is now running in the background.
In this example, Ctrl+Z
stops the sleep
command. jobs
lists the suspended job. bg %1
sends job 1 (the sleep
command) to the background. The &
symbol is often used with commands to run them in the background directly, but it’s not necessary here as we’re already using bg
. You can now continue using your terminal.
Example 2: Backgrounding a Custom Script
Let’s imagine you have a script named long_running_script.sh
that performs a lengthy calculation.
$ ./long_running_script.sh
^Z
$ jobs
[1]+ Stopped ./long_running_script.sh
$ bg %1
[1]+ ./long_running_script.sh &
The steps are identical: suspend with Ctrl+Z
, list jobs with jobs
, and move to the background with bg
.
Example 3: Using Job Numbers and Job Names
You can also specify the job using its job number (e.g., %1
, %2
) or its command name (e.g., %sleep
). If multiple jobs have the same command name, using the job number is safer.
$ sleep 60 &
[1] 12345 # Assuming PID 12345
$ sleep 30 &
[2] 67890 # Assuming PID 67890
$ jobs
[1]+ Running sleep 60 &
[2]- Running sleep 30 &
^Z
$ jobs
[1]- Stopped sleep 60
[2]+ Stopped sleep 30
$ bg %sleep
[1]+ sleep 60 &
$ bg %2
[2]+ sleep 30 &
This example shows how you can use bg
with multiple jobs, highlighting the usage of job numbers and the command name.
Important Note: If your background process needs to interact with the terminal (e.g., requesting user input), it might not function correctly. For such processes, consider using tools like nohup
or screen
.
Use the jobs
command to monitor your background processes. The kill
command (with appropriate signals) can be used to stop them if needed. We will cover these commands in detail in future articles.