Mastering the “Kill” Command in Linux – Linux

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 “ that you can use to terminate the application forcefully. In this tutorial we will show you the various ways you can make use of the “kill” command to terminate an application.

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:

linux-kill-list-command

  • 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:

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

or

where the -9 flag refers to SIGKILL signal.

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

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

kill-find-pid

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

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

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:

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

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

linux-kill-pgrep-command

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:

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. If you would like, you can also add a keyboard shortcut to activate the xkill function.

When apps misbehave and cause the system to hang, it is very tempting to restart the computer and start the session all over again. With these “kill” commands you will be able to better manage the misbehaving apps without them causing the system to crash. This is especially useful for a server when you don’t want a misbehaving process to bring the whole server down.

Image credit: Kill Bill (Gates)

You might also like
Leave A Reply

Your email address will not be published.