How to kill process by ID

Everything that is currently running on your Linux system is a process. Some processes are meant to run in the background (application updates, for example), so you may not be readily aware of their existence. And other processes (a web browser, for example) are very apparent, and get started or stopped by the user on a constant basis. All of these processes are assigned an ID number, called PID or “Process ID”, and can be referenced in various situations, including killing a running process via the process ID.

In this tutorial, you will learn how to kill a process by the ID number using the kill Linux command. While there are usually “cleaner” methods for closing a process, killing it can prove useful if a process is hung up or frozen, or is causing some kind of damage that justifies its immediate termination. Let’s look at a few examples below to see how we can kill a process by ID.

In this tutorial you will learn:

  • How to see a list of running processes
  • How to kill a process via PID number
How to kill process by ID
How to kill process by ID
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software kill, ps, top
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Find a Process ID Number




In order to kill a process via its ID number, we must first know which number has been assigned to the relevant process. The ps command is the best way to ascertain a process ID number. Using this command with the -e option should give us all the information we need to know about the running processes on our system.

$ ps -e
Using the ps command to see process ID numbers on Linux
Using the ps command to see process ID numbers on Linux

The left column shows all of the PID numbers for each process. This can produce a lot of output, because even a fresh installation of Linux can have quite a few running processes, so it is usually better to pipe this command to grep or less.

For a more manageable view:

$ ps -e | less

Or to look for a specific process (ssh in this example):

$ ps -e | grep ssh

Another handy way to see a list of running processes and their PIDs is with the top command. Sometimes this is even more useful, since it also shows how much system resources are being used by the processes.




See the PID with top command
See the PID with top command

How to Kill a Process by ID

Once you have determined the PID of the process you wish to end, you can specify it as an argument to the kill command. For example, to end a process with a PID of 1234:

$ kill 1234

This command sends a SIGTERM signal and should be able to shut down most processes within a second or two. Remember that you can use the ps command again to verify that the process has closed successfully.

$ ps -e | grep 1234

If the process refuses to close, you can force it to close immediately by sending a SIGKILL signal to the process. This can be accomplished by using the -9 option:

$ kill -9 1234
WARNING
When possible, you should send an ordinary SIGTERM signal to a process that you wish to end. Immediately killing a service with a SIGKILL signal is effective every time and will give you instant results, but can cause future problems since the process did not get a chance to shut down properly.

Other Kill Options

Normally, SIGTERM and SIGKILL are probably the only two signals you will need to use, but there are many others that you can send with the kill command. The following command will output a full list:

$ kill -L
A list of the signals that kill can send
A list of the signals that kill can send

It is good to at least be aware of the other options, since some programs or scripts may recommend their use. As you can see in the output, SIGKILL is listed at #9, which is why the -9 switch sends the SIGKILL signal. Specify a different number to send its respective signal to a process.



Closing Thoughts

In this tutorial, we saw how to kill any process running on a Linux system by using the kill command. We saw how to identify the process ID for any running process, and then use this information to terminate the process. We learned about the SIGTERM and SIGKILL signals, and how to determine which signal is better for a given scenario. Armed with this information, you can now close any process on your Linux system, either gracefully with SIGTERM or abruptly with SIGKILL.