Linux bg Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the bg command in Linux to manage background processes. The bg command is used to move a suspended (stopped) job to the background, allowing you to continue working in the foreground. You will learn how to suspend a foreground process, move it to the background, and list and manage background processes. The lab covers the purpose and syntax of the bg command, as well as practical examples of its usage.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/ProcessManagementandControlGroup -.-> linux/jobs("`Job Managing`") linux/ProcessManagementandControlGroup -.-> linux/fg("`Job Foregrounding`") linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") linux/ProcessManagementandControlGroup -.-> linux/killall("`Multi-Process Killing`") linux/ProcessManagementandControlGroup -.-> linux/pkill("`Pattern-Based Killing`") linux/ProcessManagementandControlGroup -.-> linux/wait("`Process Waiting`") linux/ProcessManagementandControlGroup -.-> linux/bg_running("`Background Running`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/jobs -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/fg -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/sleep -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/kill -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/killall -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/pkill -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/wait -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/bg_running -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} linux/bg_process -.-> lab-422574{{"`Linux bg Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the bg Command

In this step, you will learn about the purpose and syntax of the bg command in Linux. The bg command is used to move a suspended (stopped) job to the background, allowing you to continue working in the foreground.

To begin, let's start a process in the foreground and then suspend it using the Ctrl+Z key combination:

$ sleep 60
^Z
[1]+  Stopped                 sleep 60

As you can see, the sleep 60 command has been suspended and is now in the background.

To move the suspended process to the background, use the bg command:

$ bg
[1]+ sleep 60 &

The bg command resumes the suspended process and moves it to the background, allowing you to continue working in the foreground.

You can also use the bg command with a job number to move a specific suspended process to the background. For example, if you have multiple suspended processes, you can use bg 2 to move the second job to the background.

Example output:

[1]+ sleep 60 &

The syntax for the bg command is:

bg [job_id]

Where job_id is the optional job number of the suspended process you want to move to the background. If no job number is specified, the bg command will move the most recently suspended process to the background.

Suspend a Foreground Process and Move it to the Background

In this step, you will learn how to suspend a foreground process and move it to the background using the bg command.

First, let's start a long-running process in the foreground:

$ sleep 120

While the sleep 120 command is running, press Ctrl+Z to suspend the process:

^Z
[1]+  Stopped                 sleep 120

Now, the sleep 120 process is suspended and in the background. To move it to the background, use the bg command:

$ bg
[1]+ sleep 120 &

The bg command resumes the suspended process and moves it to the background, allowing you to continue working in the foreground.

Example output:

[1]+ sleep 120 &

You can also list the background processes using the jobs command:

$ jobs
[1]+ Running                 sleep 120 &

This shows that the sleep 120 process is now running in the background.

List and Manage Background Processes

In this step, you will learn how to list and manage background processes using the jobs and fg commands.

First, let's start a few background processes:

$ sleep 60 &
[1] 12345
$ sleep 120 &
[2] 12346
$ sleep 180 &
[3] 12347

To list the currently running background processes, use the jobs command:

$ jobs
[1]   Running                 sleep 60 &
[2]   Running                 sleep 120 &
[3]   Running                 sleep 180 &

The jobs command shows the job number, status, and command of each background process.

You can also get more detailed information about the background processes using the jobs -l command:

$ jobs -l
[1] 12345 Running                 sleep 60 &
[2] 12346 Running                 sleep 120 &
[3] 12347 Running                 sleep 180 &

This includes the process ID (PID) of each background process.

To bring a background process to the foreground, use the fg command followed by the job number:

$ fg 2
sleep 120

The fg command brings the specified background process to the foreground, allowing you to interact with it.

To stop a background process, you can use the kill command with the process ID (PID) obtained from the jobs -l command:

$ kill 12346
[2]+ Terminated              sleep 120

This will terminate the background process with PID 12346.

Summary

In this lab, you learned about the purpose and syntax of the bg command in Linux, which is used to move a suspended (stopped) job to the background, allowing you to continue working in the foreground. You practiced suspending a foreground process using Ctrl+Z and then moving it to the background using the bg command. You also learned how to list and manage background processes, which can be useful for running long-running tasks without tying up your terminal session.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like