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.
- 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
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Ubuntu, Debian, CentOS, RHEL, Fedora|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
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
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 -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
For a more manageable view:
# ps -e | more -15
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.
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 -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
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
pkill command is capable of sending different signals, just like the regular
# 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.
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.