Linux / Unix: fg Command Examples

by on January 13, 2013 · 0 comments· LAST UPDATED February 2, 2014

in , , , ,

fg command

I am new Linux/Unix shell user. How do I bring a background process to the foreground on Linux or Unix-like system? How do I run jobs in the foreground on Linux/Unix bash/ksh or POSIX based 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.

fg command details
DescriptionBring a background process
to the foreground
Category
Difficulty
Root privilegesNo
Estimated completion time10m
Contents
You shell keeps a table of currently executing jobs and can be displayed with jobs command. You need to use bg command to restart a stopped background process. The fg command moves a background job in the current shell environment into the foreground.

Purpose

Place job in the foreground, and make it the current job.

Syntax

The basic syntax is as follows:

fg jobID

OR

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

fg command examples

Before you start using fg command, you need to start couple of jobs on your system for demonstration purpose. Type the following commands to start jobs:

xeyes &
gnome-calculator &
gedit fetch-stock-prices.py &

Finally, run ping command in foreground:

ping cyberciti.biz

To suspend ping command job hit the Ctrl-Z key sequence.

Finding jobs that are running or suspended in background

Type the following jobs command:
$ jobs -l
The output of the jobs -l command shows the following job running in the background:

[4]+  6138 Stopped                 ping cyberciti.biz

How do I bring a background ping command job to the foreground?

To bring a background or suspended process called ping to the foreground, enter:
$ fg %4
OR
$ fg %ping
The screen displays ping command as follows:

Fig.01 fg command in action

Fig.01 fg command in action

fg command options

From the bash(1) command man page or run help fg command to see all options:

 
fg: fg [job_spec]
    Move job to the foreground.
 
    Place the job identified by JOB_SPEC in the foreground, making it the
    current job.  If JOB_SPEC is not present, the shell's notion of the
    current job is used.
 
    Exit Status:
    Status of command placed in foreground, or failure if an error occurs.
 

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

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

fg is a shell builtin
fg is /usr/bin/fg

In almost all cases you need to use the fg command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/fg command can not be used in the current shell 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: