Introduction to Linux Background Processes with the bg Command
In this tutorial, you'll discover how to effectively use the bg
command within a Linux environment for managing background processes. The bg
command is your key to moving a suspended (or stopped) job into the background, freeing up your foreground terminal for other tasks. We will cover suspending a foreground process, seamlessly transitioning it to the background, and employing commands to list and oversee these background processes. This lab provides a detailed exploration of the bg
command's purpose, its syntax, and practical, real-world examples of its application, essential knowledge for any systemadmin.
Understanding the bg Command: Purpose and Syntax
This section provides a deep dive into the core function and proper syntax of the bg
command in Linux. As mentioned, the bg
command allows you to shift a currently suspended job from occupying the foreground to running unobtrusively in the background.
Let's begin by initiating a process in the foreground and then suspending it. You can achieve this using the Ctrl+Z
key combination:
$ sleep 60
^Z
[1]+ Stopped sleep 60
As you can see from the output, the sleep 60
command has been successfully suspended and is currently paused.
To resume this suspended process and move it to the background, simply execute the bg
command:
$ bg
[1]+ sleep 60 &
The bg
command now resumes the suspended process, detaches it from the foreground, and runs it in the background, granting you the freedom to continue working without interruption.
The bg
command can also target specific suspended processes by referencing their job number. If you have multiple jobs suspended, for instance, you can use bg 2
to move the job labeled "2" into the background.
Example output showcasing the successful move to background:
[1]+ sleep 60 &
The fundamental syntax of the bg
command is as follows:
bg [job_id]
Here, job_id
represents the optional job number that identifies the specific suspended process you wish to move. If you omit the job_id
, the bg
command will automatically target the most recently suspended process, placing it in the background.
Suspending a Foreground Process and Moving it to the Background using bg
This section demonstrates how to deliberately suspend a foreground process and then leverage the bg
command to smoothly relocate it to the background.
First, let's initiate a process designed to run for an extended duration in the foreground:
$ sleep 120
While the sleep 120
command is actively running, press Ctrl+Z
. This action will immediately suspend the process:
^Z
[1]+ Stopped sleep 120
At this point, the sleep 120
process is suspended. Use the bg
command to move this suspended process to the background:
$ bg
[1]+ sleep 120 &
Now, the bg
command has resumed the sleep 120
process and moved it to the background, freeing your terminal for other tasks.
Example output showing the process running in the background:
[1]+ sleep 120 &
You can verify the background processes using the jobs
command:
$ jobs
[1]+ Running sleep 120 &
This confirms that the sleep 120
process is now actively running in the background.
Listing and Managing Background Processes in Linux
In this section, we'll explore how to list and manage background processes using the jobs
and fg
commands, essential skills for any Linux systemadmin.
Let's initiate multiple background processes to demonstrate:
$ sleep 60 &
[1] 12345
$ sleep 120 &
[2] 12346
$ sleep 180 &
[3] 12347
To view a list of currently active background processes, use the jobs
command:
$ jobs
[1] Running sleep 60 &
[2] Running sleep 120 &
[3] Running sleep 180 &
The jobs
command provides information about each background process, including its job number, current status, and the command that is being executed.
For more detailed information, including the process ID (PID), use the jobs -l
command:
$ jobs -l
[1] 12345 Running sleep 60 &
[2] 12346 Running sleep 120 &
[3] 12347 Running sleep 180 &
This output displays the process ID (PID) of each background process, a crucial piece of information for more advanced management tasks.
To bring a background process back to the foreground, use the fg
command followed by the corresponding job number:
$ fg 2
sleep 120
The fg
command will retrieve the specified background process and return it to the foreground, allowing you to directly interact with it.
To terminate a background process, use the kill
command along with the process ID (PID) that you obtained from the jobs -l
command. As root, you can terminate any process.
$ kill 12346
[2]+ Terminated sleep 120
This command will terminate the background process identified by the PID 12346.
Summary: Mastering Background Processes in Linux
Throughout this lab, you've gained a comprehensive understanding of the bg
command in Linux. You've learned its core purpose, which is to move a suspended job to the background, enabling you to continue using your terminal for other tasks. You've also practiced suspending processes using Ctrl+Z
and then seamlessly moving them to the background with the bg
command. Moreover, you now possess the skills to list and effectively manage background processes, which is essential for running long-running tasks without tying up valuable terminal sessions. This knowledge is crucial for any systemadmin working with Linux environments.