Linux Kill Process Example

Hello readers! A process on a Linux system is defined as an occurrence of a running application or task. In this tutorial, we will learn how to terminate a suspended or hung up process or an entire process group using the kill command in Linux.

1. Introduction

A process on a Linux system is defined as an occurrence of a running application or task. When a process is executing, it keeps switching it state from running (i.e. the process is either executing or just set to be executed) or waiting (i.e. the process is waiting for an event or a system resource).

Want to master BASH scripting?
Subscribe to our newsletter and download the BASH Programming Cookbook right now!
In order to help you start using BASH scripting, we have compiled a kick-ass guide with all the major commands and use cases! Besides studying them online you may download the eBook in PDF format!
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

1.1 Kill command in Linux

A kill command is a command line utility for terminating a suspended or hung up or an entire process group. The kill command sends a TERM signal to the specified process allowing the process to perform any clean-up operations before the shutdown. The kill command usually terminates or restarts the process and has the following prototype form:

Basic syntax

# kill <pid>

Where:

Do remember:

If developers want to have a detailed look at the available list of Linux signals, they can refer this link.

2 Practical usage

Let’s understand the usage of this command with the help of the sample snippets.

2.2.1 Start Linux

Start a standalone Linux instance as shown below.

Fig. 1: Start Linux instance

2.2.2 Identify the process id of an application

To terminate a process, developers need to find the Process Identification Number (i.e. PID) of an executing process or the process that needs to be terminated. To identify the process identification number (i.e. pid) of an application, developers can execute the ps -ef command to display the list of applications that are currently running on the Linux system. The following Linux command can be used.

Query 1

# ps –ef

The command gives the following output.

Fig. 2: Process id all programs

Now, in this tutorial, we are running the email program and let’s say we wish to terminate the program. To identify the process identification number of a specific program, developers can use the grep command in conjugation to the ps –ef command. The following Linux command can be used.

Query 2

# ps –ef | grep mutt

The command gives the following output.

Fig. 3: Process id of a specific program

2.2.3 Terminating a process

Once the process identification number is known for a program, let us now take a look at how to kill the process. In this example, I want to terminate the mutt program, so I’ll do it as follows:

Query 3

# kill <_Process_id_of_mutt_program_>

The kill command sends a TERM signal indicating that the process should be terminated. The operating system catches this signal and handles the process termination gracefully such as releasing the resources or saving the program state. The command gives the following output.

Fig. 4: Terminate the email process

Let’s say instead of specifying a process by its process id, developers can also specify the name of the process. If more than one process runs with the same name, all of them will be terminated. The following Linux command can be used.

Query 4

# killall mutt

2.2.4 Verifying a process

To verify that the process has been successfully terminated, developers can run the pidof command and they will not be able to view the process identification number of the terminated program.

Query 5

# pidof <_application_name_>

The command gives the following output.

Fig. 5: Verifying the terminated process

That’s all for this post. Happy Learning!!

3. Conclusion

In this tutorial, developers learned how to terminate an existing process in the Linux system using the kill command.

Developers can download the sample application as an Eclipse project in the Downloads section.

4. Download the Eclipse Project

This was a tutorial of kill command in Linux.

Download
You can download the full source code of this example here: Commands
Exit mobile version