This tutorial explains Linux “bg” command, options and its usage with examples.
DESCRIPTION
Continues a stopped job in the background. A stopped job will occur if the connection is interrupted or the commanded is interrupted with TSTP signals (typically CTRL + Z). This function is not available on all Unix shell’s.
SYNOPSIS
bg [jobid]
jobid
Specifies the job that you want to run in the background.
The JobID parameter can be a process ID number, or you can use one of the following symbol combinations:
%Number
Refers to a job by the job number.
%String
Refers to a job whose name begins with the specified string.
%?String
Refers to a job whose name contains the specified string.
%+ OR %%
Refers to the current job.
%-
Refers to the previous job.
EXAMPLES
1. Sending the current foreground job to the background using CTRL-Z and bg command
You can send an already running foreground job to background as explained below:
Press ‘CTRL+Z’ which will suspend the current foreground job.
Execute bg to make that command to execute in background.
For example, if you’ve forgot to execute a job in a background, you don’t need to kill the current job and start a new background job. Instead, suspend the current job and put it in the background as shown below.
# find / -ctime -1 > /tmp/changed-file-list.txt # [CTRL-Z] [2]+ Stopped find / -ctime -1 > /tmp/changed-file-list.txt # bg
2. Make Specific job to run in background
If the output of the jobs command displays the following stopped job:
[2] + Stopped (SIGSTOP) sleep 100 &
use the job number to resume the sleep 100 & job by entering:
# bg %2
OR
# %2 &
The screen displays the revised status of job 2:
[2] sleep 100 &
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Apply for Programming Internship
- Check Linux Books
- Practice Programming MCQs
- Check Information Technology Books