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.
In this tutorial, we learn about fg command in Linux.
Prerequisites
Access to a terminal
A normal user or sudo user account.
Linux fg command
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 the 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, the fg command can move a background job to the foreground. That will make a particular job become the current job running in that shell session. It is the opposite of the bg command which moves a foreground job to the background.
The fg command might be 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
In this case, it is available as a shell built-in command.
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 to the fg command. 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 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 fg command’s 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
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.
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 with the fg command. Let's resume the ping to the ‘yahoo.com’ job which is identified by the 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 which is used to start the job containing a string ‘s’ can be specified as follows.
fg %?s
The '--help' option can be used to display additional information related to the fg command as well.
Conclusion
In summary, the fg command is one of the job control commands that has been used to move a Linux job to the foreground. As mentioned, this command should be available as a built-in shell command. The fg command can be called without any arguments that will move the current job to the foreground. Also, a specific job can be referenced by specifying the job_spec argument. Overall, the fg command is very useful in managing Linux jobs.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
About The Author
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.
Comments