Before we begin talking about how to use fg command on Linux, let's briefly understand – What is fg command?
The fg (ForeGround) command brings processes that are currently running in the background to the foreground. To run a process in the foreground, job control must be enabled. The service used to manage the running processes, including the starting, stopping, and restarting of the stopped processes, is known as job control.
In this tutorial, you will use fg command on Linux. We will also address a few FAQs on how to use fg command on Linux.
How Does the fg Command Work on Linux?
Users of Linux distributions must adhere to the general syntax outlined below when using the fg command:
The command above has the following explanation:
- To start, use "fg" to cancel the command.
- The job_spec, which is supposed to be a change from the background to the foreground, should then be specified.
The fg command's explanation is provided in the table below. Additional symbols can also be used with this command:
|The previous job is displayed using this.
|These are utilized to show the active job.
|The job number is displayed using this.
|To handle the job with the command, use this.
|This indicates the task that was launched along with the command.
We will now use a few examples to better understand how to use the fg command.
Example of the fg Command on Linux
We must first execute a few computer jobs before describing how to use the fg command. Run the command shown below to accomplish this:
Use the keyboard shortcut CTRL+Z to exit the xeyes command. Ping vegastack's URL using the following command:
And use the shortcut key "CTRL+Z" to end the ping task. List down all the commands after that to ensure that they were all the running jobs:
The two jobs, whose work IDs are presented, can be seen being terminated in the backdrop.
Example 1: Bring the Background Job to Foreground
In the example above, we'll resume the ping job using its job number, which is "1," and to do so, we must use the command. The fg command can be used to highlight the background jobs.
The ping operation has been continued.
Example 2: Bring the Background Job to Foreground Through the String
In the example above, the work has been resumed using the job number, but you can also do this with the string. As an example, let's use the command to move it from the background to the foreground:
The ping command has started working again.
Example 3: Bring the Background job to Foreground Through the Job ID
Using the work id of the background job, such as 3829 in our case, is the third way to bring the job to the foreground. This id can be used to highlight the process as well:
Example 4: Bring the Previous job to Foreground
You might also emphasize your previous job. Use the following command, for instance, to rerun the ping job with the fg command:
FAQs to Use fg Command in Linux
How can I determine the job or process ID of a background process?
To determine the job or process ID of a background process, you can use the
jobs command or the
ps command with appropriate options.
Can I use the
fg command with multiple background processes?
fg command only supports bringing one background process to the foreground at a time. You need to specify the desired job or process ID.
What happens when I bring a process to the foreground using
When a process is brought to the foreground using
fg, it resumes execution and its output becomes visible in the current terminal session.
Can I use the
fg command with suspended processes?
Yes, you can use the
fg command to bring suspended processes to the foreground. It will resume their execution.
How do I bring a background process to the foreground if I don't know its job or process ID?
If you don't know the job or process ID, you can use the
% symbol followed by a string from the process name to bring it to the foreground. For example,
What if the background process is already running in the foreground?
If the background process is already running in the foreground, using the
fg command won't have any effect.
Is there an alternative to the
Yes, an alternative to the
fg command is using the
kill -CONT command followed by the process ID to resume a suspended process and bring it to the foreground.
The general syntax for the fg command on Linux is "fg [Job]". On Linux, you may use a variety of symbols to highlight particular jobs. Different examples have been used in this tutorial to demonstrate how to utilize the fg command on Linux using various symbols.