Linux / Unix: bg Command Examples

by on January 23, 2012 · 0 comments· LAST UPDATED February 2, 2014

in , , , ,

bg command

I am new Linux and Unix system bash/ksh user. How do I run jobs or scripts in the background on Linux/Unix-like systems? How can I run jobs in the background on bash or ksh or sh shell?

Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.

bg command details
DescriptionRuns jobs in
the background
Category
Difficulty
Root privilegesNo
Estimated completion time5m
Contents

The bg command is part of Linux/Unix shell job control. The command may be available as both internal and external command. It resumes execution of a suspended process as if they had been started with &. Use bg command to restart a stopped background process.

Purpose

Resumes suspended jobs in the current environment by running them as background jobs.

Syntax

The basic syntax is as follows:

bg jobID
bg jobID1 jobID2 ... jobIDN

Understanding the job number (jobID)

There are a various ways to refer to a job in the shell. The character % introduces a job specification. The JobID can be a process ID (PID) number, or you can use one of the following symbol combinations:

  1. %Number : Use the job number such as %1 or %2.
  2. %String : Use the string whose name begins with suspended command such as %commandNameHere or %ping.
  3. %+ OR %% : Refers to the current job.
  4. %- : Refers to the previous job.

bg command examples

There are two other commands (key sequence) you need to know before you start using bg command.

How do I find status of jobs in the current session?

Simply use the jobs command as follows to list all active jobs in the current bash/ksh/tcsh shell:
$ jobs
OR
$ jobs -l
Sample outputs:

[1]   6107 Running                 gedit fetch-stock-prices.py &
[2]-  6148 Running                 gnome-calculator &
[3]+  6155 Stopped                 ping cyberciti.biz

In this example, three jobs are output: gedit fetch-stock-prices.py (gui based text editor), gnome-calculator, and suspend/stopped ping cyberciti.biz command.

How do I suspend or stop jobs in the current session?

A job is suspended simply by using the Ctrl-Z short cut key sequence or using kill command or pkill command:

 
kill -s stop PID
kill -s stop jobID
pkill -stop PID
 

Putting is all tougher

In this example, you are going to run ping command command in foreground:

 
ping www.cyberciti.biz
 

To suspend ping command job hit the Ctrl-Z key sequence. To list the active jobs, enter:
$ jobs -l
Sample outputs:

Fig.01: Suspending and listing the current job

Fig.01: Suspending and listing the current job


The output of the jobs command displays the following stopped job:

[3]+  Stopped                 ping cyberciti.biz

Now, use the job number 3 to resume the ping cyberciti.biz job by entering:

bg %3

Sample outputs:

[3]+ ping cyberciti.biz &
nixcraft@wks05:~$ 64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=4 ttl=53 time=264 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=5 ttl=53 time=250 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=6 ttl=53 time=251 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=7 ttl=53 time=251 ms
64 bytes from www.cyberciti.biz (75.126.153.206): icmp_req=8 ttl=53 time=267 ms

Here is another example that run a Unix shell script called update-mutual-fund.sh command:

 
~/scripts/scripts/financial/update-mutual-fund.sh --all --output=html
 
### To stop press CTRL + Z ##
jobs -l
 
#### ~/scripts/scripts/financial/update-mutual-fund.sh has job id # 6 
### Resume job id #6 
bg %6
 

In this example, run gnome-calculator in background from the Terminal for calculation:
$ gnome-calculator &
Sample outputs:

[1] 6517

To stop job ID # 1 (Or PID # 6517), type the following kill command:
$ kill -s stop %1
OR
$ kill -s stop 6517
Sample outputs:

[1]+  Stopped                 gnome-calculator

Once stopped you will not able to punch numbers into gnome-calculator. To resume stopped gnome-calculator, enter:
$ jobs -l
$ bg %1

OR
$ jobs -l
$ bg %gnome-ca

Sample outputs:

[1]+ gnome-calculator &

bg command options

From the bash(1) command man page or type the following command:
$ help bg
Sample outputs:

 
bg: bg [job_spec ...]
    Move jobs to the background.
 
    Place the jobs identified by each JOB_SPEC in the background, as if they
    had been started with `&'.  If JOB_SPEC is not present, the shell's notion
    of the current job is used.
 
    Exit Status:
    Returns success unless job control is not enabled or an error occurs.
 

A note about /usr/bin/bg and shell builtin

Type the following type command to find out whether bg is part of shell, external command or both:
$ type -a bg
Sample outputs:

bg is a shell builtin
bg is /usr/bin/bg

In almost all cases you need to use the bg command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/bg command can not be used in the current session. The /usr/bin/bg command operates in a different environment and does not share the parent bash/ksh's shells understanding of jobs.

Related media

This tutorial is also available in a quick video format:

See also
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdighostip
Processes Managementbgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searchingwhereiswhich
User Informationgroupsidlastlastcommlognameuserswwhowhoamilidmembers

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: ,

Previous Faq:

Next Faq: