Kill a Process in Linux

Last Updated 2022-12-13

Finding and Killing Processes

The [.inline-code]pkill[.inline-code] command can be used to stop / kill all processes that match its pattern from the command line:

[.inline-code]pkill -aif warp[.inline-code]

If you want to be more precise, the [.inline-code]kill[.inline-code] command can be used to quit a particular process by specifying its process ID:

[.inline-code]kill 2934[.inline-code]

There are a number of ways you can discover the IDs of running processes depending on what you know about the process.

[#searching-for-the-right-process]Searching For The Right Process To Kill[#searching-for-the-right-process]

You can search for a process by:

  • The command name that was executed to spawn it:
    [.inline-code]pgrep -aifl firefox[.inline-code]
  • The user that executed it:
    [.inline-code]pgrep -u root[.inline-code]
  • A network port it's using:
    [.inline-code]lsof -i tcp:8888[.inline-code]
  • A file it has open:
    [.inline-code]lsof /dev/null[.inline-code]

[#force-killing-processes]Force Killing Processes[#force-killing-processes]

If the process doesn't end, you can force kill the process by sending it the [.inline-code]SIGKILL[.inline-code] signal. To do this, simply add the [.inline-code]-9[.inline-code] argument to [.inline-code]kill[.inline-code] or [.inline-code]pkill:[.inline-code]

[.inline-code]pkill -9 -aif firefox[.inline-code]

[#pkill-vs-pgrep-vs-killall-vs-ps][.inline-code]pkill[.inline-code] and [.inline-code]pgrep[.inline-code] vs. [.inline-code]killall[.inline-code] and [.inline-code]ps[.inline-code][#pkill-vs-pgrep-vs-killall-vs-ps]

[.inline-code]killall[.inline-code] and [.inline-code]ps[.inline-code] are the old-fashioned equivalents of [.inline-code]pkill[.inline-code] and [.inline-code]pgrep[.inline-code]. They're now generally discouraged because they behave very differently and are somewhat hard to use. Some versions of Linux may not even provide [.inline-code]killall[.inline-code].

If you still want to use [.inline-code]killall[.inline-code], you might have difficulty targeting the right process, but you can try the following:

[.inline-code]killall Firefox[.inline-code]

[#tip-for-warp-users]💡Tip for Warp Users[#tip-for-warp-users]

If these commands don't find any matching processes, they will often silently exit with no notice that it didn't work other than a nonzero return code. Warp users will notice that the terminal block is helpfully made red to indicate that the process failed.

[#process-signals]Process Signals[#process-signals]

By default, running [.inline-code]kill[.inline-code] or [.inline-code]pkill[.inline-code] sends the [.inline-code]SIGTERM[.inline-code] signal to the specified process, which is the polite way to ask the process to terminate. The process can choose to ignore or handle this signal rather than ending, either purposefully or because the process is misbehaving.

Signals you can send include:

  • 2 - [.inline-code]SIGINT[.inline-code] - Interrupt the program (equivalent to pressing Ctrl+C on the terminal)
  • 9 - [.inline-code]SIGKILL[.inline-code] - Forcibly terminate the program immediately (cannot be ignored or handled)
  • 15 - [.inline-code]SIGTERM[.inline-code] (default) - Request that the program terminate (can be ignored or handled)

[#command-portability]Command Portability[#command-portability]

Some of these commands and their flags may behave slightly differently on different operating systems. Favoring [.inline-code]pkill[.inline-code] (instead of [.inline-code]killall[.inline-code]) and [.inline-code]pgrep[.inline-code] (instead of [.inline-code]ps[.inline-code]) can help reduce these issues. All of the specific invocations listed above will work on both Linux and MacOS.