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.
The following is the basic syntax of the fg command.
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.
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.
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.
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.
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.
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.
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’.
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.
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:
Also, the command that is used to start the job containing a string ‘s’ can be specified as follows.