-->

How to Kill a Process by Name in Linux

Author : Piyush Gupta

Command line users rely on the ‘kill’ command to terminate a process as defined by the appropriate process identifier (PID). While there’s nothing wrong with targeting processes by their PID, another approach which is often easier is to target a process by name, rather than its unique identifier.

There are a few ways to kill a process by process name, we’ll review two primary methods using killall and pkill. These will work the same in Mac OS / X and linux, and they can be used to target GUI apps and processes as well as those running in the background or exclusively at the command line. Either command can be prefixed with sudo to terminate root level tasks or those owned by another user.

Killing a Process by Name with killall

The killall command is the most commonly used way to kill a process by its name:
  • From the Terminal, type the following command (in this example using task “process name” as the targeted process to kill)
killall <process name>
  • Hit return to instantly kill the ‘process name’ process (replace "process name" with any other process name to kill it)
  NOTE: killing a process is instantaneous and unforgiving, it immediately terminates the process without saving any data. This can result in data loss and other irregularities if you’re not sure what you’re doing.

Kill a Process by Name with pkill

The pkill command also offers an approach to terminate processes by name rather than targeting a PID. One of the perks of pkill is that it makes it easier to target processes with spaces in their names since you only need to use quotes around the task name to kill.
  • From the Terminal, type the following command:
pkill <process name>
  • Hit Return to immediately terminate the named process
    NOTE: Like killall, pkill will immediately terminate the process that has been targeted with no confirmations, dialog, saves, or anything else. 

Examples:

For example to kill all processes running with name java, use the following command.
sudo pkill java
To kill all processes running with name httpd use the following command.
sudo pkill httpd

No comments:

Post a Comment