Linux kill Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux kill command to terminate processes and send signals to running processes. The lab covers the purpose of the kill command, how to terminate processes using the command, and explores advanced options for the kill command. You will start long-running processes and then use the kill command to terminate them, as well as learn about the different signals that can be sent to processes. This lab provides practical examples and a solid understanding of the kill command, which is an essential tool for process management in Linux.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/BasicSystemCommandsGroup -.-> linux/sleep("`Execution Delaying`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/ProcessManagementandControlGroup -.-> linux/kill("`Process Terminating`") subgraph Lab Skills linux/sleep -.-> lab-422753{{"`Linux kill Command with Practical Examples`"}} linux/grep -.-> lab-422753{{"`Linux kill Command with Practical Examples`"}} linux/ps -.-> lab-422753{{"`Linux kill Command with Practical Examples`"}} linux/kill -.-> lab-422753{{"`Linux kill Command with Practical Examples`"}} end

Understand the Purpose of the kill Command

In this step, you will learn about the purpose of the kill command in Linux. The kill command is used to terminate or send signals to running processes.

The kill command allows you to:

  • Terminate a process by sending a signal to it
  • Send various signals to a process, such as SIGTERM (default), SIGINT, SIGKILL, etc.
  • Terminate processes by their process ID (PID) or process name

Let's start by running a simple command that creates a long-running process:

sleep 1000 &

This will start a sleep process that runs for 1000 seconds in the background.

To view the PID of the sleep process, run:

ps aux | grep sleep

Example output:

labex      5678  0.0  0.0   8192   728 pts/0    S    12:34   0:00 sleep 1000

The PID of the sleep process is 5678.

Now, let's terminate this process using the kill command:

kill 5678

The sleep process should have terminated immediately.

Terminate Processes Using the kill Command

In this step, you will learn how to terminate processes using the kill command.

First, let's start a new long-running process:

sleep 1000 &

This will start a new sleep process that runs for 1000 seconds in the background.

To view the PID of the sleep process, run:

ps aux | grep sleep

Example output:

labex      5678  0.0  0.0   8192   728 pts/0    S    12:34   0:00 sleep 1000

The PID of the sleep process is 5678.

Now, let's terminate this process using the kill command:

kill 5678

The sleep process should have terminated immediately.

To verify that the process has been terminated, run the following command again:

ps aux | grep sleep

The output should not show the sleep process anymore.

Explore Advanced kill Command Options

In this step, you will explore some advanced options of the kill command.

The kill command can send different signals to processes. The default signal is SIGTERM, which requests the process to terminate gracefully. However, you can also send other signals, such as SIGINT (interrupt), SIGKILL (immediate termination), and more.

Let's start by creating a new long-running process:

sleep 1000 &

Now, let's terminate the process using the SIGINT signal:

kill -SIGINT 5678

The sleep process should have been interrupted and terminated.

You can also use the signal number instead of the signal name:

kill -2 5678

This will send the SIGINT signal to the process with PID 5678.

Another useful option is the -9 option, which sends the SIGKILL signal. This signal cannot be ignored by the process and will terminate it immediately:

kill -9 5678

The SIGKILL signal is a "last resort" option when the process does not respond to other signals.

Summary

In this lab, you learned about the purpose of the kill command in Linux, which is used to terminate or send signals to running processes. You started by running a long-running sleep process and then terminated it using the kill command with the process ID (PID). You also explored advanced options of the kill command, such as sending different signals to processes, terminating processes by their name, and using the killall command to terminate multiple processes at once.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like