How to Kill a Running Process on Linux

Everything that’s running on a Linux system – a service, script, or anything else – is considered a “process.” If you need to end a running process on Linux, the kill command is sure to do the job.

In this guide for Linux administrators, we’ll go over the kill Linux command and how to use its various options to end, or “kill,” a running process on Linux.

In this tutorial you will learn:

  • How does the kill command work?
  • How to see what processes are running
  • How to kill a process by PID
  • How to kill a process that refuses to close
  • How to kill a process by name

How to Kill a Running Process on Linux

How to Kill a Running Process on Linux

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu, Debian, CentOS, RHEL, Fedora
Software None
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 does the kill command work?

With a name like “kill,” you might expect that this utility is used to immediately end a process. While this is true, kill only functions in that manner when used with certain options. By default, kill will try to stop a process as gracefully as possible.

In technical terms, kill sends a SIGTERM signal to the specified process, and that instructs the process to shut down. It’s a polite way to end a running process, and gives the application or service time to wrap things up first – like finish writing to log files, close opened connections that were spawned by the process, etc.



The only problem with this is that a stubborn program may ignore the SIGTERM signal. This occurs especially in a process that is frozen or “hung up.” However, kill can be used to send a lot of different signals to a process, and can even force the most stubborn ones to close with a SIGKILL signal. We’ll go over these options in the next section.

The important thing to take away from this information is that, 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 didn’t get a chance to shut down properly.

How to see what processes are running

Every process in Linux is assigned a unique “process ID,” commonly abbreviated as PID in the Linux terminal. Sometimes, knowing the PID of a process is necessary in order to end it. To see a list of running processes, and their corresponding PID numbers, use the ps command:

# ps -e

This can produce a lot of output, because even a fresh installation of Linux can have quite a few running processes, so it’s usually better to pipe this command to grep or more.

For a more manageable view:

# ps -e | more -15


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

# ps -e | grep ssh
Get the PID of a running process

Get the PID of a running process

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 PID



Once you’ve 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

How to kill a process that refuses to close

If you’ve encountered a stubborn process that is refusing to close, you can issue a SIGKILL signal with the -9 option on kill.



# kill -9 1234

Just remember to use this option sparingly, as it’s not the ideal way to end a process (but sometimes you have to).

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’s 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.



How to kill a process by name

There’s a faster way to end a process than having to look up its PID every time. Use the pkill command and the name of the process you wish to kill. For example, here’s how to kill SSH:

# pkill ssh

The pkill command is capable of sending different signals, just like the regular kill command:

# pkill -9 ssh

Don’t worry about getting the exact name of the process, either. This command killed a process named ssh-agent that was running on our system.



There’s one drawback to using pkill instead of kill. Say you have two SSH processes running, ‘ssh-server’ and ‘ssh-agent’. Issuing the command above with pkill is going to end both of these processes, even if you only intended to end ssh-agent.

It’s also a problem if you have multiple instances of a script running, since telling pkill to end that script is going to end all instances of the script. Then again, sometimes this behavior may be preferable. If you need the more granular control, just remember to opt for the kill command instead.

Conclusion

In this article, we saw how to kill any process running on a Linux system by using the kill command. We learned how to kill a process either by its PID or by name, along with the pros and cons of both methods.

With knowledge of the kill command and the best practices for how to use it, you have more control over the processes running on your system. This is particularly handy for programs that “hang up” and refuse to close sometimes.



Comments and Discussions
Linux Forum