fg Command in Linux

Written by: Nimesha Jinarajadasa   |   Last updated: August 1, 2022

In Linux, a collection of tasks that are attached to the shell can be identified as a job. Furthermore, a job can be a collection of processes at its lowest level which has started and not finished yet. Each of these jobs is assigned with a job id to identify uniquely. Linux jobs can be run in the background or foreground of your current shell session. It is possible to send a job running in the background to the foreground of the shell using the fg command that stands for the foreground.

The fg command is one of the widely used job control commands that is used with commands such as jobs, bg, etc. Usually, the currently running jobs can be suspended by the key combination Ctrl + Z. It would stop the currently running foreground job and give back control to the bash shell. Also, it will be assigned with a job id.

Executing the fg command will resume the stopped job and continue to run it in the foreground. In addition, it can move a background job to the foreground. That will make a particular job become the current job running in that shell session. It does the opposite of the bg command which moves a foreground job to the background.

Syntax

The following is the basic syntax of the fg command.

fg [job_spec]

The fg command can be used without a job_spec. It is an optional argument. Hence, if you use the ‘fg’ command without any options, it will operate on the current job being stopped or running in the background.

fg is available in your Linux system as a built-in or external binary file. The type command can be used to examine it.

type fg
Display the type of fg command
Display the type of fg command

In this case, it is available as a shell built-in command.

fg command example

Before making use of the fg command, we need to have some running jobs in the shell environment. In this case, we are using a bash shell to demonstrate the behavior.

The jobs command is used to inspect the currently running or suspended jobs in a given shell session.

jobs

Since we haven't started any jobs, this command will return nothing. Let’s initiate a couple of jobs from the shell. We will be using the ping command as a demo job here.

ping google.com

The ping command will continuously send data packets to the specified IP address until the user terminates the job. 

Next, the Ctrl + Z key combination can be used to suspend the currently running ping job and the shell control will be given back as shown in the following.

In the same way, let’s ping ‘yahoo.com’. Then suspend that job as well.

Now we can use the jobs command to check the list of suspended jobs for this session and their job ids. The -l option can be used with the jobs command which displays each job’s process group id(PID), process id number, state, etc.

jobs -l
Lists process IDs in addition to the normal information

If you check the job ids closely, the ‘+’ and ‘-’ signs are there for two jobs that have been suspended. The ‘+’ sign means that it is the current job. Also, the previous job is denoted by the ‘-’. 

Let’s use the fg command to resume the current job. Since we call the fg command without any optional arguments, it will pick the current job from the list and resume it, and move to the foreground.

run fg command

Furthermore, you can use the bg command to move this job to the background and then the fg command to take it back to the foreground.

fg options

Two main options are available to use with the fg command. 

A job on the current shell session can be referenced using a job_spec. This job_spec can be specified in different forms as shown in the following. The job_spec should be started with a ‘%’ sign.

The %+ and %- can be used to reference the current and previous jobs associated with the current shell environment.

In the previous example, we can use the following command to resume the previous job which is the ping against ‘google.com’. 

fg %-

Furthermore, you can use the job id directly. Let's resume the ping to the ‘yahoo.com’ job which is identified by job number 2.

fg %2

It is possible to reference a job by the command that is used to start it. You can specify the command beginning with the string ‘s’ as shown in the following:

fg %s

Also, the command that is used to start the job containing a string ‘s’ can be specified as follows.

fg %?s

About The Author

Nimesha Jinarajadasa

Nimesha Jinarajadasa

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called “03 tiers(DB, M-Tier, and Client)”. Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment