Background Job Management

LinuxLinuxBeginner
Practice Now

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 jobs command
  • Bring background jobs to the foreground using the fg command
  • Stop background jobs using the kill command
  • Understand the terminal-specific nature of background jobs

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/ProcessManagementandControlGroup -.-> linux/jobs("`Job Managing`") linux/ProcessManagementandControlGroup -.-> linux/fg("`Job Foregrounding`") linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") linux/RemoteAccessandNetworkingGroup -.-> linux/ping("`Network Testing`") linux/ProcessManagementandControlGroup -.-> linux/bg_process("`Background Management`") subgraph Lab Skills linux/jobs -.-> lab-43{{"`Background Job Management`"}} linux/fg -.-> lab-43{{"`Background Job Management`"}} linux/sleep -.-> lab-43{{"`Background Job Management`"}} linux/tar -.-> lab-43{{"`Background Job Management`"}} linux/find -.-> lab-43{{"`Background Job Management`"}} linux/kill -.-> lab-43{{"`Background Job Management`"}} linux/ping -.-> lab-43{{"`Background Job Management`"}} linux/bg_process -.-> lab-43{{"`Background Job Management`"}} end

Running a Command in the Background

Let's start by running a simple command in the background.

  1. Open your terminal. You should be in the /home/labex/project directory. If not, you can change to this directory by typing:

    cd /home/labex/project
  2. We'll use the sleep command to simulate a long-running process. Run the following command:

    sleep 600 &

    The sleep command 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.

  3. After running this command, you'll see output similar to this:

    [1] 1234

    This output shows two important pieces of information:

    • [1] is the job number. This is used to refer to this specific background job.
    • 1234 is the process ID (PID). This is a unique identifier for the running process.
  4. 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.

  5. Now, let's check the status of our background job:

    jobs

    This 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 600 command we just started.

  6. 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 jobs there 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.

  1. 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.

  2. Now, let's check the status of all our background jobs:

    jobs

    You 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.

  3. 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.

  1. First, list your current jobs:

    jobs

    This refreshes your view of what's currently running in the background.

  2. 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 fg command followed by the job number:

    fg %3

    The %3 refers to the job number. The % symbol tells the shell that you're referring to a job number, not a process ID.

  3. 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.

  4. To stop the job and regain control of your terminal, press Ctrl+C. This sends an interrupt signal to the current foreground process.

  5. Let's check our jobs again:

    jobs

    You 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.

  1. First, list your current jobs:

    jobs

    This shows you which jobs are still running.

  2. 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 kill command followed by the job number:

    kill %2

    The %2 refers to the job number you've chosen. Again, the % symbol indicates that we're referring to a job number.

  3. The kill command sends a termination signal to the specified job. By default, it sends a signal that allows the process to shut down gracefully.

  4. Check the jobs again:

    jobs

    You 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.

  1. We'll use the tar command 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.

  2. You'll see a job number and PID, just like with our previous commands.

  3. Check the status of the job:

    jobs

    You should see the tar command listed as a running job.

  4. While it's running, you can continue to use your terminal for other tasks. This is the power of background jobs!

  5. After a while, check if the command has completed:

    jobs

    If it's no longer listed, it has finished.

  6. Now, let's verify that the archive was created:

    ls -l home_backup.tar.gz

    This 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:

  1. Running commands in the background using the & operator
  2. Listing and monitoring background jobs with the jobs command
  3. Bringing background jobs to the foreground with fg
  4. Stopping background jobs using kill
  5. 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.

Other Linux Tutorials you may like