Linux fg Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the fg command in Linux to bring background processes to the foreground. The lab covers the purpose of the fg command, how to bring a background process to the foreground, and how to manage multiple background processes using the fg command. You will learn practical examples and gain a better understanding of process management in the Linux operating system.

The lab starts by explaining the purpose of the fg command and how it is used to interact with background processes. It then demonstrates how to start a background process and bring it to the foreground using the fg command. Finally, the lab covers managing multiple background processes by using the fg command with the process ID (PID) or job number.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/ProcessManagementandControlGroup -.-> linux/jobs("`Job Managing`") linux/ProcessManagementandControlGroup -.-> linux/fg("`Job Foregrounding`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/jobs -.-> lab-422679{{"`Linux fg Command with Practical Examples`"}} linux/fg -.-> lab-422679{{"`Linux fg Command with Practical Examples`"}} linux/bg_process -.-> lab-422679{{"`Linux fg Command with Practical Examples`"}} end

Understand the Purpose of the fg Command

In this step, you will learn about the purpose of the fg command in Linux. The fg command is used to bring a background process to the foreground, allowing you to interact with it directly.

In Linux, when you run a command, it can either run in the foreground or the background. Foreground processes are the ones you can interact with directly, while background processes run without user interaction.

To start a process in the background, you can append the & character at the end of the command. For example:

sleep 60 &

This will start the sleep command in the background, and you'll get the process ID (PID) of the background process.

Example output:

[1] 12345

Now, to bring this background process to the foreground, you can use the fg command:

fg

This will bring the most recent background process to the foreground, allowing you to interact with it.

If you have multiple background processes, you can use the fg command with the PID or the job number (the number in the square brackets) to bring a specific process to the foreground. For example:

fg 12345

or

fg %1

Both of these commands will bring the background process with the PID 12345 or the job number 1 to the foreground.

Bring a Background Process to the Foreground

In this step, you will learn how to bring a background process to the foreground using the fg command.

First, let's start a background process:

sleep 60 &

This will start the sleep command in the background, and you'll get the process ID (PID) of the background process.

Example output:

[1] 12345

Now, to bring this background process to the foreground, you can use the fg command:

fg

This will bring the most recent background process to the foreground, allowing you to interact with it.

If you have multiple background processes, you can use the fg command with the PID or the job number (the number in the square brackets) to bring a specific process to the foreground. For example:

fg 12345

or

fg %1

Both of these commands will bring the background process with the PID 12345 or the job number 1 to the foreground.

Once the process is in the foreground, you can interact with it directly. For example, if you started the sleep command in the background, you can now interrupt it by pressing Ctrl+C.

Manage Multiple Background Processes with fg

In this step, you will learn how to manage multiple background processes using the fg command.

Let's start by creating a few background processes:

sleep 60 &
sleep 120 &
sleep 180 &

This will start three sleep commands in the background. You can see the job numbers and process IDs (PIDs) of the background processes:

Example output:

[1] 12345
[2] 12346
[3] 12347

Now, to bring a specific background process to the foreground, you can use the fg command with the job number or the PID:

fg %2

This will bring the background process with job number 2 (the sleep 120 command) to the foreground.

If you want to switch between multiple background processes, you can use the fg command repeatedly:

fg %1
## Interrupt the first process by pressing Ctrl+C
fg %3
## Interrupt the third process by pressing Ctrl+C

This will allow you to switch between the different background processes and interact with them directly.

You can also use the PID instead of the job number to bring a specific process to the foreground:

fg 12347

This will bring the background process with PID 12347 (the sleep 180 command) to the foreground.

By learning how to manage multiple background processes with the fg command, you can optimize your workflow and efficiently switch between different tasks running in the background.

Summary

In this lab, you will learn about the purpose of the fg command in Linux, which is used to bring a background process to the foreground, allowing you to interact with it directly. You will also learn how to manage multiple background processes using the fg command, including how to bring a specific background process to the foreground by specifying its process ID (PID) or job number. The key learning points covered in this lab include understanding the difference between foreground and background processes, starting a process in the background, and using the fg command to bring a background process to the foreground.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like