2024-04-18
Before exploring bg, understand background processes. These are processes that run independently of your current terminal session, allowing you to continue working on other tasks without interruption. You can initiate a background process using the ampersand (&) symbol at the end of a command. For instance:
sleep 60 &This command starts a sleep process for 60 seconds in the background. Immediately after executing this, you can type other commands and the sleep process will continue running independently.
However, if a long-running command is interrupted (e.g., by pressing Ctrl+Z), it moves to a stopped state. This is where bg becomes invaluable.
bgThe bg command takes a job ID as its argument. This job ID is a number assigned by the shell to each job it manages. You can view the list of currently running and stopped jobs using the jobs command:
sleep 1000 & #Starts a long sleep process in background
sleep 10 & #Starts a shorter sleep process in background
jobsThis might output something like:
[1]+ Running sleep 1000 &
[2]+ Running sleep 10 &
Now, let’s interrupt the first sleep process (job 1):
Ctrl+Z
jobsThis will stop the process, and jobs might show:
[1]+ Stopped sleep 1000
[2]+ Running sleep 10 &
Now, use bg to resume the stopped job:
bg %1
jobsThe %1 refers to job 1. You can also use % followed by the job name if it’s unique or use the % followed by a portion of the job name if it’s not unique. After running bg %1, jobs will likely display job 1 as running again.
Let’s say you have many stopped jobs:
sleep 100 &
sleep 200 &
Ctrl+Z
Ctrl+Z
jobsThis might show:
[1]+ Stopped sleep 100
[2]+ Stopped sleep 200
You can resume them individually using bg %1 and bg %2, or you can resume both simultaneously by using bg %1 %2
bg Without ArgumentsIf you run bg without any arguments, it resumes the most recently stopped job. This is a convenient shortcut.
While bg is straightforward, remember that background processes consume system resources. Excessive background processes can degrade performance. Always monitor your system resource usage and manage background processes appropriately using commands like top or htop. Furthermore, ensure your scripts and commands handle potential errors and unexpected terminations when running them in the background.