How to kill a process with the kill command in Linux

Tasks in Linux are called processes. Each process has a unique process ID. To end a process in Linux we can use the kill command. In this tutorial, we’ll show you how to cancel a process using , to improve your .

Sometimes you can start a server or application, forget about it, and then need to shut it down. In these scenarios, you can use the kill command.

Below are some examples where the kill command can be useful:

  • To stop any automated process.
  • To stop a process that has been started by accident.
  • To stop a process that consumes a lot of memory.
  • To force stop any process running on Linux.
  • To stop a background process.

Besides stopping a process, the kill command has other functions. Like, for example, sending a signal to a process. By default, this is taken as a TERM signal, which will terminate the process.

How to display the process ID in Linux

The kill command allows you to kill a process using a specific process ID, also known as a pid. To display a pid on Linux, you can run the following command:

$

This will list all available processes and their pids. If you want your list to be more specific, add a like this:

ps -ux | grep java

This will show all running Java processes in the output.

How to show all the signals of the Kill command

There are multiple signals available in Linux that can be used to interrupt, terminate, or pause processes. The kill command can be used as follows:

kill -l

This command will display a page with the different signals of the kill command, with their corresponding names and numbers. Although there are several signals available, in most cases SIGKILL (9) and SIGTERM (15) are used.

See also  What is a database query?

How to kill a process in Linux using the terminal

Now we are ready to move on and learn the different uses of the kill command. To continue, access your VPS server using .

Use the Kill command with a PID

To end a specific process with a P.I.D.use the following command:

kill 63772

Here 63772 is the pid of the process we want to kill, you can replace it with the corresponding pid. Since no token is specified, this will be a token SIGTERM. Sometimes this may not work; in that case, you may have to force kill the process.

For that you can use the format of the kill command shown below:

kill pid

We give you an example of how to forcefully close a process using this format:

kill SIGKILL 63772

Similarly, if you want a shorter version of this command you can use:

kill -9 63772

As we mentioned before, you must replace 63772 with the corresponding pid for the process to finish.

How to kill multiple processes in Linux

With Linux kill you can also kill multiple processes. The syntax for the command in this case would be:

kill -9 pid1 pid2 pid3

We give you an example of what it would look like when applied:

kill -9 63772 45116 23465

How to kill a process in Linux using the Pkill command

Pkill is a variation of the kill command. With this variation you can specify the process name or a pattern to find a process:

pkill chrome

The above command will close the Chrome browser. You can also specify a partial match of the process name on the command line. Example:

See also  WooCommerce Tutorial: How to Install eCommerce in WordPress

pkill chr

However, this command has the risk of killing the wrong process, especially when there are multiple processes with the same name.

You can query the list of processes that the command might consider using the full process name:

pidof chrome

This command can be used when you know the full name of the process.

You can also check for matching processes with a partial name:

pgrep -l chr

This command will list the processes that match “chr”, along with the process ID.

How to Kill a Process in Linux Using the Killall Command

The basic difference between killall and kill is that killall can kill the process by name while the kill command uses the pid.

An example of this command is:

killall chrome

This is similar to pkill. However, killall does an exact name match, while pkill can do a pattern match. This is one of the reasons why killall is more secure compared to pkill.

One more difference is the root package that these commands belong to. On Linux, killall belongs to the psmisc package. While on the other hand commands like ps, top, kill, pkill belong to the procps package.

Another difference is that killall can be customized to kill processes based on timestamps. In case you want to kill a specific process that has been running for less than 40 minutes, you can use:

killall -y 40m

Similarly, you can use the following options in conjunction with the killall command:

  • s – seconds
  • m – minutes
  • h – hours
  • d – days
  • w weeks
  • M – months
  • and – years
See also  What does customer loyalty mean to ?

To end

With this tutorial we cover the kill command along with its most important and useful variations. For more information about this essential tool, you can refer to the Linux manual. Good luck with your project, see you in the next tutorial!

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...