How to KILL a process on Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Introduction

To get full use of your system you must watch what’s going on and monitor the utilization of the system.

Linux is not a perfect world, of course it utilizes your resources better, but as any other operating system there may be some resources that get out of control or stop responding or maybe consuming a lot of resources or more than they need.

For this we’re here to talk about how to take the action and terminate the disobeying tasks when the normal ways do not come in handy.

There are lots of ways to control your system and one of them which is our topic today is KILL command

What is KILL command?

Kill command is a built-in utility in Linux that allows you to terminate processes manually. It sends a signal to the hang-up process to terminate it.

There are different types of signal that KILL command can send to the process,

to display them run the following command:

You can specify the signals in the command options by either of three ways:

● Using number

kill -15 or kill -s 15

● Typing the SIG prefix

kill -SIGTERM or kill -s SIGTERM

● Without SIG prefix

kill -TERM or kill -s TERM

If you don`t specify any signal, TERM (-15) signal is the default signal.


To locate a process

Before you terminate the process, you need to identify its PID (Process ID),

to do so there are variant ways

PS command

You can locate the process unique number by using PS command under the PID section

To get more information about PS command and its options please visit this article

How to use PS command

TOP command

Find PID also in TOP command under the first section

To get more information about PS command and its options please visit this article

How to use TOP command

PIDOF and PGREP

If you want a way to search by name to find the corresponding ID, you can use those two commands.

PIDOF is straightforward, you have to know the full name of the process to get all PIDs for this exact name

if you don’t know the full name of the process, here you can use PGREP to get the PID


Processes you can’t KILL

If you are a normal user, you can only KILL your own processes, however root user can terminate all processes. Use sudo before the command to act as a super user

When you kill a process, a TERM signal is sent by default as we mentioned before, this allows the program to run some codes before it exits, thus that gives the program a chance to exit gracefully. If you want to terminate the process forcibly, use SEGKILL instead

Linux kernel keeps some information about the terminated processes so that the parent process can find out the state of the child processes and whether it was terminated by itself or it was killed. These zombie processes will appear in the processes list and you cannot KILL them

When a process perform input or output operations, it is in an uninterruptible sleep and you can’t KILL these processes either

You can distinguish processes in state of zombie or uninterruptible sleep by looking at the state section in top and see Z or D respectively.


Now, let’s KILL the process

After you located the PID, simply run the following command

You can either identify the TERM signal in the options or send another signal to the program

here a KILL signal was sent

KILL by name

If you know the name of the process, you can kill it by name using PKILL command

this will kill any process than contains chrome in it

Also you can identify the signal type

KILL all processes

You can kill all processes that have the same name with no need to locate the PID,

one of the easiest way to kill a process

it’s similar to pkill command and also you can send different types of signals

You can terminate a process that has been running for more than a period of time using the –o option

killall -o 30m process_name

or killing a process that has been running for less than period of time as the following

killall -y 10s process_name

notice that m in 30m represents minutes and s in 10s is for seconds.


Conclusion

As for any other operating systems, Linux might has some hanging or not responding programs and thus you need KILL command to terminate the processes that consume the resources of the system.

To get more information, use man for kill command and see the official documentation.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Mel
Melhttps://unixcop.com
Unix/Linux Guru and FOSS supporter

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook