Linux / Unix: bg Command Examples

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.[donotprint]

bg command details
Description Runs jobs in
the background
Root privileges No
Estimated completion time 5m

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.


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


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
$ jobs -l
Sample outputs:

[1]   6107 Running                 gedit &
[2]-  6148 Running                 gnome-calculator &
[3]+  6155 Stopped                 ping

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

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

A job is suspended simply by using the CtrlZ 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:


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

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

[3]+  Stopped                 ping

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

bg %3

Sample outputs:

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

Here is another example that run a Unix shell script called command:

~/scripts/scripts/financial/ --all --output=html
### To stop press CTRL + Z ##
jobs -l
#### ~/scripts/scripts/financial/ 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
$ 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

$ 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.

This tutorial is also available in a quick video format:

See also
  • bash(1) Linux/Unix command man page
  • ksh(1) Linux/Unix command man page
Category List of Unix and Linux commands
File Management cat
Network Utilities dighostip
Package Manager apkapt
Processes Management bgchrootdisownfgjobskillkillallpwdxtimepidofpstree
Searching whereiswhich
User Information idgroupslastlastcommlognameuserswwhowhoamilid/libuser-lidmembers

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts