Linux Process Terminating

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn essential Linux process management skills, focusing on how to identify and terminate processes. Process management is a fundamental skill for any Linux user or administrator, as it allows you to manage system resources effectively.

You will learn how to use the ps command to identify running processes and the kill command to terminate them when necessary. These tools are essential for maintaining system performance and dealing with unresponsive applications.

By the end of this lab, you will be able to identify processes, understand their attributes, and terminate them using various methods. These skills are valuable for troubleshooting and managing Linux systems efficiently.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/ProcessManagementandControlGroup(["Process Management and Control"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("Execution Delaying") linux/ProcessManagementandControlGroup -.-> linux/kill("Process Terminating") linux/ProcessManagementandControlGroup -.-> linux/killall("Multi-Process Killing") linux/ProcessManagementandControlGroup -.-> linux/bg_process("Background Management") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") subgraph Lab Skills linux/sleep -.-> lab-271315{{"Linux Process Terminating"}} linux/kill -.-> lab-271315{{"Linux Process Terminating"}} linux/killall -.-> lab-271315{{"Linux Process Terminating"}} linux/bg_process -.-> lab-271315{{"Linux Process Terminating"}} linux/ps -.-> lab-271315{{"Linux Process Terminating"}} end

Identifying Processes

In Linux, every running program or application is considered a process. Each process has a unique identifier called a Process ID (PID). Before you can terminate a process, you need to identify it using its PID.

Let's start by learning how to view running processes using the ps command. This command displays information about active processes in your system.

First, open a terminal in the LabEx VM environment and run the following command:

ps

You will see a basic list of processes that are running in your current terminal session. The output should look similar to this:

  PID TTY          TIME CMD
 2104 pts/0    00:00:00 zsh
 2116 pts/0    00:00:00 ps

This output shows:

  • PID: The Process ID number
  • TTY: The terminal type the process is running on
  • TIME: The amount of CPU time the process has used
  • CMD: The command that started the process

To see a more comprehensive list of all processes running on the system, use the following command with additional options:

ps -aux

The options mean:

  • -a: Show processes from all users
  • -u: Display detailed user-oriented format
  • -x: Include processes without a controlling terminal

The output will be much longer and more detailed, showing all processes running on the system:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.1 167936 11876 ?        Ss   10:30   0:01 /sbin/init
root           2  0.0  0.0      0     0 ?        S    10:30   0:00 [kthreadd]
...
labex       2104  0.0  0.1  11108  5456 pts/0    Ss   10:35   0:00 zsh
labex       2130  0.0  0.0   9828  3384 pts/0    R+   10:36   0:00 ps -aux

Now, let's create a simple background process that we can use for practice:

sleep 300 &
sleep_pid=$!

This command starts a process that will "sleep" (do nothing) for 300 seconds (5 minutes). The & symbol runs it in the background, and $! captures the PID of the most recently started background process.

Let's verify the process is running:

echo "The sleep process ID is: $sleep_pid"
ps | grep sleep

You should see output similar to:

The sleep process ID is: 2135
 2135 pts/0    00:00:00 sleep

Now you know how to identify processes and find their PIDs. In the next step, we'll learn how to terminate this process.

Terminating Processes with 'kill'

Now that you've identified a process and its PID, you can learn how to terminate it using the kill command. The kill command sends a signal to a process, instructing it to terminate.

The basic syntax for the kill command is:

kill [options] <PID>

Let's terminate the sleep process we created in the previous step. We stored its PID in the $sleep_pid variable. Use the following command to terminate it:

kill $sleep_pid

If you've forgotten the PID, you can find it again using:

ps | grep sleep

After executing the kill command, verify that the process has been terminated:

ps | grep sleep

If the process was successfully terminated, you should see no output or only the grep process itself in the output. This means the sleep process is no longer running.

If you want to be thorough, you can use the ps command again to list all processes:

ps -aux | grep sleep

This might show something like:

labex       2156  0.0  0.0   7236   720 pts/0    S+   10:40   0:00 grep --color=auto sleep

The only process shown is the grep command itself, which indicates the sleep process has been terminated.

By default, the kill command sends a TERM (terminate) signal to the process, which allows the process to exit gracefully after performing any cleanup operations. In most cases, this is sufficient to terminate a process.

You've now successfully identified and terminated a process using the kill command, which is a fundamental skill for managing processes in Linux.

Understanding Kill Signals

In Linux, the kill command can send various types of signals to processes, not just termination signals. Understanding these signals gives you more control over how processes are terminated.

First, let's see all available signals by running:

kill -l

This will list all the signals that the kill command can send. The output looks like:

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
...

The most commonly used signals are:

  • SIGTERM (15): Default signal sent by the kill command. Asks the process to terminate gracefully.
  • SIGKILL (9): Forces the process to terminate immediately. Use when a process doesn't respond to SIGTERM.
  • SIGHUP (1): Traditionally used to reset or reload configuration files.
  • SIGINT (2): Sent when you press Ctrl+C in the terminal.

Let's create another sleep process to practice with:

sleep 300 &
sleep_pid=$!
echo "The new sleep process ID is: $sleep_pid"

Now, let's send a specific signal to the process using the signal number:

kill -15 $sleep_pid

This is equivalent to the default kill command as it sends SIGTERM (signal 15).

Verify that the process has been terminated:

ps | grep sleep

Let's create one more sleep process to demonstrate the SIGKILL signal:

sleep 300 &
sleep_pid=$!
echo "The third sleep process ID is: $sleep_pid"

Now, let's use the SIGKILL signal to forcefully terminate the process:

kill -9 $sleep_pid

Verify that the process has been terminated:

ps | grep sleep

The SIGKILL signal (9) is a "last resort" option because:

  1. It does not allow the process to clean up after itself
  2. It might leave resources in an inconsistent state
  3. It might cause data loss if the process was writing data

Always try SIGTERM first, and only use SIGKILL if the process is unresponsive to SIGTERM.

Advanced Process Management

Linux provides additional commands for managing processes beyond the basic ps and kill commands. These tools provide more flexibility and convenience for identifying and terminating processes.

The pgrep Command

The pgrep command allows you to find processes by name rather than having to visually scan the output of ps:

pgrep sleep

This command will find all processes with "sleep" in their name and output their PIDs.

The pkill Command

The pkill command combines the functionality of pgrep and kill - it finds processes by name and sends a signal to them:

## Start two sleep processes
sleep 300 &
sleep 300 &

## Kill all sleep processes
pkill sleep

Verify that all sleep processes have been terminated:

ps | grep sleep

The killall Command

Similar to pkill, the killall command terminates processes by name:

## Start two more sleep processes
sleep 300 &
sleep 300 &

## Kill all sleep processes
killall sleep

Verify once more:

ps | grep sleep

Process Priority with nice and renice

Linux also allows you to control the priority of processes using the nice and renice commands.

Let's start a process with a lower priority:

nice -n 10 sleep 300 &

The nice value ranges from -20 (highest priority) to 19 (lowest priority). The default is 0. Only the root user can set negative nice values.

You can change the priority of a running process using renice:

sleep_pid=$!
echo "Sleep process ID with lower priority: $sleep_pid"
renice -n 15 -p $sleep_pid

Let's check the new priority:

ps -o pid,nice,cmd -p $sleep_pid

Finally, terminate this process:

kill $sleep_pid

These advanced process management commands provide more efficient ways to manage multiple processes and control system resources, which is especially useful on busy systems or when troubleshooting performance issues.

Summary

In this lab, you have learned essential Linux process management skills that are valuable for system administration and troubleshooting:

  1. Identifying Processes: You learned how to use the ps command to list running processes and find their Process IDs (PIDs).

  2. Terminating Processes: You practiced using the kill command to send termination signals to processes using their PIDs.

  3. Understanding Kill Signals: You explored different types of signals that can be sent to processes, including SIGTERM (15) for graceful termination and SIGKILL (9) for forced termination.

  4. Advanced Process Management: You discovered more powerful tools like pgrep, pkill, and killall for finding and terminating processes by name, as well as how to control process priority with nice and renice.

These skills enable you to:

  • Monitor system processes effectively
  • Terminate unresponsive applications
  • Manage system resources
  • Troubleshoot performance issues

Process management is a fundamental aspect of Linux system administration, and the commands you learned in this lab are used daily by Linux administrators and users to maintain system performance and stability.