Linux Job Managing

LinuxBeginner
Practice Now

Introduction

In this lab, you will learn the fundamentals of Linux job management. Job management is an essential skill for Linux users, allowing you to run multiple processes simultaneously and control their execution. By mastering job management, you can efficiently run commands in the background, switch between foreground and background tasks, and maintain control over all running processes.

Throughout this lab, you will learn:

  • How to start processes in the background
  • How to view and manage running jobs
  • How to bring background jobs to the foreground
  • How to suspend running jobs and resume them later
  • How to terminate jobs when they are no longer needed

These skills are particularly useful when working with long-running tasks that don't require constant interaction, allowing you to continue working on other tasks while processes run in the background.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 91% completion rate. It has received a 97% positive review rate from learners.

Starting and Managing Background Jobs

In this step, you will learn how to start a process in the background using the ampersand (&) symbol and how to manage it using the jobs command.

What is a Background Job?

In Linux, a job is a process that the shell manages. By default, when you run a command in a terminal, it runs in the foreground - meaning your terminal is busy until the command completes. However, by adding the & symbol at the end of a command, you can run it in the background, freeing your terminal for other tasks.

Creating a Background Process

Let's create a simple script that will run continuously in the background. This will simulate a long-running task that you might want to run while continuing to work on other things.

First, navigate to the project directory if you're not already there:

cd ~/project

Now, create a script called background_task.sh using the nano text editor:

nano ~/project/background_task.sh

Inside the editor, add the following content:

#!/bin/bash
## A simple script that simulates a long-running task
count=0
while true; do
  count=$((count + 1))
  echo "Task running: iteration $count"
  sleep 5
done

This script will run indefinitely, printing a message every 5 seconds.

Press Ctrl+O followed by Enter to save the file, then Ctrl+X to exit nano.

Next, make the script executable:

chmod +x ~/project/background_task.sh

Now run the script in the background by adding the & symbol at the end of the command:

~/project/background_task.sh &

You should see output similar to:

[1] 1234

The number in brackets [1] is the job number, and the number after it is the process ID (PID). These numbers may be different on your system.

Checking Running Jobs

To view all currently running background jobs, use the jobs command:

jobs

You should see output similar to:

[1]  + running    ~/project/background_task.sh

This confirms that your script is running in the background.

To get more detailed information about the job, including its PID, use:

jobs -l

The output should show the job number, PID, and status:

[1]  + 1234 running    ~/project/background_task.sh

Your terminal remains free for you to run other commands while the script continues to execute in the background. The script's output is still sent to the terminal, so you'll see messages appearing periodically.

Bringing Background Jobs to the Foreground

In this step, you will learn how to bring a background job to the foreground using the fg command. This is useful when you need to interact with a process that's currently running in the background.

What is Foregrounding?

Foregrounding refers to the process of bringing a background job to the foreground, making it the active process in your terminal. When a job is in the foreground, it has control of your terminal - you can interact with it directly, but you can't run other commands until it completes or is moved back to the background.

Using the fg Command

Make sure you still have your background_task.sh script running from the previous step. You can verify this with:

jobs

You should see your script listed as a running job.

To bring the job to the foreground, use the fg command followed by the job number with a percent sign:

fg %1

If you only have one background job, you can simply use:

fg

After executing the command, the script will be brought to the foreground, and you'll see its output directly in your terminal:

Task running: iteration X
Task running: iteration X+1
...

You'll notice that your command prompt disappears, and the script now has control of your terminal. You can no longer enter other commands until you either terminate the script or move it back to the background.

Stopping the Foreground Job

To stop the foreground job and return to the command prompt, press Ctrl+C. This sends an interrupt signal to the process, causing it to terminate.

^C

The terminal should now display a message indicating that the job was terminated, and you should see your command prompt again.

To verify that the job is no longer running, run:

jobs

There should be no output, indicating that no background jobs are running.

Let's start our script in the background again for the next step:

~/project/background_task.sh &

You should see confirmation that the job has started, with output similar to:

[1] 1345

Remember, the job number and PID may be different on your system.

Suspending and Resuming Jobs

In this step, you will learn how to suspend a running foreground job and resume it either in the foreground or background. This is useful when you need to temporarily pause a process without terminating it.

Suspending a Foreground Job

First, let's bring our background job to the foreground:

fg %1

You should now see the script's output in your terminal. To suspend this foreground job, press Ctrl+Z:

^Z

You should see a message similar to:

[1]  + suspended  ~/project/background_task.sh

This indicates that the job has been suspended. The job is still loaded in memory, but it is not executing - it's essentially "paused."

To verify that the job is suspended, run:

jobs

You should see output similar to:

[1]  + suspended  ~/project/background_task.sh

Resuming a Suspended Job in the Background

You can resume a suspended job in either the foreground or background. To resume it in the background, use the bg command followed by the job number:

bg %1

You should see a message confirming that the job has been resumed in the background:

[1]  + continued  ~/project/background_task.sh

Verify that the job is now running in the background:

jobs

The output should show that the job status has changed from "suspended" to "running":

[1]  + running    ~/project/background_task.sh

Resuming a Suspended Job in the Foreground

Let's suspend the job again. First, bring it to the foreground:

fg %1

Then press Ctrl+Z to suspend it:

^Z

Now, instead of using bg to resume it in the background, use fg to resume it in the foreground:

fg %1

The job will resume execution, and you'll see its output in your terminal again.

Let's stop the job by pressing Ctrl+C:

^C

This will terminate the job and return you to the command prompt.

Using Job Control in Your Workflow

The ability to suspend, resume in background, resume in foreground, and terminate jobs gives you powerful control over your processes. This workflow is particularly useful when:

  1. You need to check something else while a process is running
  2. You need to temporarily free up system resources
  3. You want to run multiple processes interactively without opening multiple terminal windows

For the next step, start the background task again:

~/project/background_task.sh &

Terminating Background Jobs

In this step, you will learn different ways to terminate background jobs when they are no longer needed. Proper job termination is important to free up system resources and maintain system performance.

Identifying Jobs for Termination

First, let's check what jobs are currently running:

jobs

You should see your background task running:

[1]  + running    ~/project/background_task.sh

If you want more detailed information, including the process ID (PID), use:

jobs -l

This will display output similar to:

[1]  + 1456 running    ~/project/background_task.sh

Note the PID (the number after the job number), as it can be used to terminate the process.

Method 1: Terminating a Job Using Its Job Number

The most straightforward way to terminate a background job is to use the kill command with the job number:

kill %1

Check if the job has been terminated:

jobs

You might see:

[1]  + terminated  ~/project/background_task.sh

If the job is still running (some processes may require a stronger termination signal), you can use:

kill -9 %1

The -9 flag sends a SIGKILL signal, which forcibly terminates the process without allowing it to clean up.

Method 2: Terminating a Job Using Its PID

Let's start another instance of our script in the background:

~/project/background_task.sh &

You should see output showing the job number and PID:

[1] 1567

To terminate this job using its PID, use:

kill 1567

Replace 1567 with the actual PID of your job.

Verify that the job has been terminated:

jobs

Method 3: Using the Process Name with killall

Let's start yet another instance of our script:

~/project/background_task.sh &

If you have multiple instances of the same process running, you can terminate all of them at once using the killall command:

killall background_task.sh

This command terminates all processes with the name background_task.sh.

Verify that no jobs are running:

jobs

There should be no output, indicating that all background jobs have been terminated.

Understanding Termination Signals

When using the kill command, you're sending a signal to the process. By default, kill sends the SIGTERM signal (signal 15), which asks the process to terminate gracefully. If a process doesn't respond to SIGTERM, you can use SIGKILL (signal 9) to force termination:

kill -TERM %1 ## Same as kill %1
kill -KILL %1 ## Same as kill -9 %1

Other useful signals include:

  • SIGHUP (signal 1): Often used to reload configuration files
  • SIGINT (signal 2): Same as pressing Ctrl+C
  • SIGSTOP (signal 19): Suspends a process (can't be caught or ignored)
  • SIGCONT (signal 18): Continues a stopped process

For one final demonstration, let's start our background task again and then terminate it:

~/project/background_task.sh &
jobs
kill %1
jobs

This sequence starts the job, confirms it's running, terminates it, and then verifies it's no longer running.

Summary

In this lab, you have learned essential skills for managing jobs in a Linux environment:

  1. Starting Background Jobs: You learned how to run processes in the background using the & symbol, which allows you to continue working in the terminal while long-running tasks execute.

  2. Checking Job Status: You used the jobs command to view information about running jobs, including their state, job number, and process ID.

  3. Foregrounding Background Jobs: You practiced bringing background jobs to the foreground using the fg command, which allows you to interact with them directly.

  4. Suspending and Resuming Jobs: You learned how to suspend running foreground jobs with Ctrl+Z and resume them in either the foreground (fg) or background (bg).

  5. Terminating Jobs: You explored different ways to terminate jobs when they are no longer needed, including using the kill command with job numbers or PIDs, and using killall to terminate multiple processes by name.

These job management skills are valuable for any Linux user, especially when working with long-running processes or when multitasking in a terminal environment. By controlling how and when processes run, you can make more efficient use of your system resources and streamline your workflow.

Remember that different signals can be used with the kill command to control process behavior, from graceful termination to forced termination, giving you fine-grained control over your system's processes.