“bg” Command Usage Examples in Linux

«
»

This tutorial explains Linux “bg” command, options and its usage with examples.

bg – Runs jobs in the background.

DESCRIPTION

advertisement

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:

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

%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:

advertisement

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:

advertisement
 # bg %2

OR

# %2 &

The screen displays the revised status of job 2:

advertisement
 [2] sleep 100 &

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.