Mastering the “Kill” Command in Linux

linux-kill-command-featured

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 tutorial 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

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:

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-find-pid

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.

linux-kill-pgrep-command

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

Conclusion

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)

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Damien Oh

Damien Oh started writing tech articles since 2007 and has over 10 years of experience in the tech industry. He is proficient in Windows, Linux, Mac, Android and iOS, and worked as a part time WordPress Developer. He is currently the owner and Editor-in-Chief of Make Tech Easier.