Saturday 6 October 2018

Mastering the “Kill” Command in Linux

Kill Command, LPI Guides, LPI Tutorial and Material, LPI Certification, LPI Study Material

It doesn’t matter which operating system you are using – you will surely come across a misbehaving application that locks itself up and refuses to close. In Linux (and Mac) there is a “kill” command that you can use to terminate the application forcefully. In this post we will show you the various ways you can make use of the “kill” command to terminate an application.

Kill Commands and Signals


When you execute a “kill” command, you are in fact sending a signal to the system to instruct it to terminate the misbehaving app. There is a total of sixty signals that you can use, but all you really need to know is SIGTERM (15) and SIGKILL (9).

You can view all the signals with the command:

kill -l

Kill Command, LPI Guides, LPI Tutorial and Material, LPI Certification, LPI Study Material

◈ SIGTERM – This signal requests that a process stop running. This signal can be ignored. The process is given time to gracefully shut down. When a program gracefully shuts down, that means it is given time to save its progress and release resources. In other words, it is not forced to stop.

◈ SIGKILL – The SIGKILL signal forces the process to stop executing immediately. The program cannot ignore this signal. Unsaved progress will be lost.

The syntax for using kill is:

kill [signal or option] PID(s)

The default signal (when none is specified) is SIGTERM. When that doesn’t work, you can use the following to kill a process forcefully:

kill SIGKILL PID

or

kill -9 PID

where the -9 flag refers to SIGKILL signal.

If you are not aware of the PID of the application, simply run the command:

ps ux

and it will display all the running applications together with its PID.

Kill Command, LPI Guides, LPI Tutorial and Material, LPI Certification, LPI Study Material

For example, to kill the Chrome app, run the command:

kill -9 3629

Do also note that you can kill multiple processes at the same time.

kill -9 PID1 PID2 PID3

PKill


The pkill command allows the use of extended regular expression patterns and other matching criteria. Instead of using PID, you can now kill applications by entering their process names. For example, to kill the Firefox browser, just run the command:

pkill firefox

As it matches a regular expression pattern, you can also enter a partial name of the process, such as:

pkill fire

To avoid killing the wrong processes, you might want to do a “pgrep -l [process name]” to list the matching processes.

Kill Command, LPI Guides, LPI Tutorial and Material, LPI Certification, LPI Study Material

Killall


Killall uses the process name as well instead of PID, and it kills all instances of the process with the same name. For example, if you are running multiple instances of the Firefox browser, you can kill them all with the command:

killall firefox

xkill


Xkill is a graphical way to kill an application. When you type xkill in the terminal, your mouse cursor will instantly become a “cross.” All you have to do is click the “cross” on the misbehaving app, and it will kill the application instantly.

Related Posts

0 comments:

Post a Comment