bg Command: Details and ExamplesThe bg command in Linux is used to resume a suspended job in the background, allowing it to continue running without occupying the terminal. This is particularly useful for multitasking in a terminal environment.
bg [job_spec]
Where job_spec is the job ID or job number (e.g., %1) of the suspended job. If no job specification is provided, the most recently suspended job is resumed in the background.
Ctrl+Z) before using bg.Start a long-running process, suspend it, and resume it in the background.
$ sleep 100
^Z
[1]+ Stopped sleep 100
$ bg
[1]+ sleep 100 &
$ jobs
[1]+ Running sleep 100 &
Explanation: The sleep 100 command is started, suspended with Ctrl+Z, and then resumed in the background with bg.
Run multiple jobs, suspend them, and resume a specific job in the background.
$ find / -name "*.txt" 2>/dev/null
^Z
[1]+ Stopped find / -name "*.txt"
$ sleep 200
^Z
[2]+ Stopped sleep 200
$ bg %1
[1]+ find / -name "*.txt" &
$ jobs
[1]+ Running find / -name "*.txt" &
[2]- Stopped sleep 200
Explanation: Two jobs are suspended. bg %1 resumes the first job (find) in the background.
Start a script, suspend it, and resume it in the background.
$ ./long_running_script.sh
^Z
[1]+ Stopped ./long_running_script.sh
$ bg
[1]+ ./long_running_script.sh &
Explanation: A script is started, suspended, and then resumed in the background with bg.
fgMove a job to the background and later bring it back to the foreground.
$ tar -zcf archive.tar.gz /home/user
^Z
[1]+ Stopped tar -zcf archive.tar.gz /home/user
$ bg
[1]+ tar -zcf archive.tar.gz /home/user &
$ fg %1
tar -zcf archive.tar.gz /home/user
Explanation: The tar command is suspended, moved to the background with bg, and later brought to the foreground with fg %1.
Verify the status of background jobs after using bg.
$ ping google.com
^Z
[1]+ Stopped ping google.com
$ bg
[1]+ ping google.com &
$ jobs
[1]+ Running ping google.com &
Explanation: The ping command is suspended and resumed in the background. jobs shows its status.
Although not using bg directly, you can start a job in the background using &.
$ sleep 300 &
[1] 12345
$ jobs
[1]+ Running sleep 300 &
Explanation: The & operator starts the job in the background immediately, bypassing the need for bg.
Manage multiple suspended jobs and resume them selectively.
$ sleep 100
^Z
[1]+ Stopped sleep 100
$ sleep 200
^Z
[2]+ Stopped sleep 200
$ bg %2
[2]+ sleep 200 &
$ jobs
[1]+ Stopped sleep 100
[2]- Running sleep 200 &
Explanation: Two jobs are suspended, and bg %2 resumes the second job in the background.
Resume a download process in the background.
$ wget http://example.com/largefile.zip
^Z
[1]+ Stopped wget http://example.com/largefile.zip
$ bg
[1]+ wget http://example.com/largefile.zip &
Explanation: A download with wget is suspended and resumed in the background.
Attempt to resume a non-existent job.
$ bg %3
bash: bg: %3: no such job
Explanation: If the specified job ID does not exist, bg returns an error.
Suspend a loop and resume it in the background.
$ while true; do echo "Running..."; sleep 1; done
^Z
[1]+ Stopped while true; do echo "Running..."; sleep 1; done
$ bg
[1]+ while true; do echo "Running..."; sleep 1; done &
Explanation: A loop is suspended and resumed in the background, continuing to print output.
Note: Use the jobs command to list all jobs and their IDs. To bring a background job to the foreground, use fg [job_spec]. To terminate a job, use kill [job_spec].