Introduction
In this lab, you will learn how to manage background jobs in Linux using various commands. You'll discover how to run processes in the background, monitor their status, and control them. This knowledge is crucial for efficient multitasking in a Linux environment, allowing you to run multiple tasks simultaneously without tying up your terminal.
Achievements
By the end of this lab, you will be able to:
- Run commands in the background using the
&operator - List and monitor background jobs using the
jobscommand - Bring background jobs to the foreground using the
fgcommand - Stop background jobs using the
killcommand - Understand the terminal-specific nature of background jobs
Running a Command in the Background
Let's start by running a simple command in the background.
Open your terminal. You should be in the
/home/labex/projectdirectory. If not, you can change to this directory by typing:cd /home/labex/projectWe'll use the
sleepcommand to simulate a long-running process. Run the following command:sleep 600 &The
sleepcommand pauses for a specified number of seconds (in this case, 600 seconds or 10 minutes). The&at the end tells Linux to run this command in the background.After running this command, you'll see output similar to this:
[1] 1234This output shows two important pieces of information:
[1]is the job number. This is used to refer to this specific background job.1234is the process ID (PID). This is a unique identifier for the running process.
You'll notice that you immediately get your command prompt back. This is because the process is running in the background, allowing you to continue using your terminal.
Now, let's check the status of our background job:
jobsThis command lists all current background jobs. You should see output like this:
[1]+ Running sleep 600 &This tells you that job number 1 is running, and it's the
sleep 600command we just started.Important Note: Background jobs are specific to the terminal session in which they were started. If you open a new terminal window or tab, running
jobsthere will not show the background jobs from your original terminal. This is a key concept in understanding how background jobs work in Linux.
Running Multiple Background Jobs
Now that we've run one job in the background, let's try running multiple jobs simultaneously. We'll use a mix of different commands to demonstrate the versatility of background jobs.
Let's start a few more background jobs. Enter these commands one after another:
ping -c 100 google.com > ping_results.txt & find /home -name "*.log" > log_files.txt & sleep 1200 &Each of these commands will start a new background job. You'll see a job number and PID for each one.
Now, let's check the status of all our background jobs:
jobsYou should see output listing all four jobs (including the one from Step 1):
[1] Running sleep 600 & [2] Running ping -c 100 google.com > ping_results.txt & [3]- Running find /home -name "*.log" > log_files.txt & [4]+ Running sleep 1200 &The
+next to job 4 indicates that this is the most recently started background job. The-next to job 3 indicates that this would become the current job if job 4 were to finish.Notice how you can start multiple background jobs quickly, and your terminal remains responsive for other commands.
Bringing a Background Job to the Foreground
Sometimes, you might want to bring a background job to the foreground, either to interact with it or to see its output. Let's learn how to do this.
First, list your current jobs:
jobsThis refreshes your view of what's currently running in the background.
We'll bring one of the long-running jobs to the foreground. Choose a job number from the list (let's say it's job number 3, but use whatever number corresponds to a job that's still running in your list). To bring it to the foreground, use the
fgcommand followed by the job number:fg %3The
%3refers to the job number. The%symbol tells the shell that you're referring to a job number, not a process ID.After running this command, you'll see the command running in the foreground, and your terminal will be "stuck" until it completes or you stop it.
To stop the job and regain control of your terminal, press
Ctrl+C. This sends an interrupt signal to the current foreground process.Let's check our jobs again:
jobsYou should see that the job you brought to the foreground is no longer listed because we stopped it.
Stopping a Background Job
Now, let's learn how to stop a background job without bringing it to the foreground.
First, list your current jobs:
jobsThis shows you which jobs are still running.
We'll stop one of the remaining background jobs. Choose a job number from the list (let's say it's job number 2, but use whatever number corresponds to a job that's still running in your list). To stop it, we use the
killcommand followed by the job number:kill %2The
%2refers to the job number you've chosen. Again, the%symbol indicates that we're referring to a job number.The
killcommand sends a termination signal to the specified job. By default, it sends a signal that allows the process to shut down gracefully.Check the jobs again:
jobsYou should see that the job you killed is no longer listed. This confirms that it has been successfully terminated.
Using Background Jobs with Other Commands
Now that we've practiced with various commands, let's try using background jobs with another practical command.
We'll use the
tarcommand to create a compressed archive of your home directory in the background:tar -czf home_backup.tar.gz /home/labex &This command creates a compressed tar archive of your home directory.
You'll see a job number and PID, just like with our previous commands.
Check the status of the job:
jobsYou should see the
tarcommand listed as a running job.While it's running, you can continue to use your terminal for other tasks. This is the power of background jobs!
After a while, check if the command has completed:
jobsIf it's no longer listed, it has finished.
Now, let's verify that the archive was created:
ls -l home_backup.tar.gzThis will show you details of the archive file if it was successfully created.
Summary
In this lab, you've learned essential skills for managing background jobs in Linux:
- Running commands in the background using the
&operator - Listing and monitoring background jobs with the
jobscommand - Bringing background jobs to the foreground with
fg - Stopping background jobs using
kill - Applying these concepts to practical scenarios, like network diagnostics, file searches, and creating archives
You've also learned an important concept: background jobs are specific to the terminal session in which they were started. This means that each terminal window or tab maintains its own set of background jobs.
These skills will help you multitask efficiently in a Linux environment, allowing you to run multiple processes simultaneously and manage them effectively. Remember, background jobs are particularly useful for long-running tasks that don't require constant interaction, freeing up your terminal for other work.



