Home » Linux » Linux Kill Process Example

About Yatin Batra

Yatin Batra
Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.

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

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:

  • pid indicates the Process Identification Number of the application which can be used to terminate the process
  • A pid of -1 is special as it indicates to terminate all processes except two i.e. the kill process itself and the root process

Do remember:

  • The kill -9 command forcefully terminates a process in the Linux system
  • To send a signal to any process in the Linux system, developers use the kill command. In Linux, the frequently used signals include the HUP, INT, KILL, STOP, CONT, and 0

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

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

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

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

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

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.

  • The kill command sends a signal to other processes in Linux
  • To display a full list of supported signals, developers can execute the kill –l 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
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
Email address:

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of