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 “ExampleTask” as the targetted process to kill)
- Hit return to instantly kill the ‘ExampleTask’ process (replace ExampleTask with any other process name to kill it)
Remember, 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:
- Hit Return to immediately terminate the named process
pkill "Example Process name agent"
Like killall, pkill will immediately terminate the process that has been targeted with no confirmations, dialogs, saves, or anything else. The process just instantly ends, much like using force quit on apps from the task manager or Activity Monitor.
pkill is a powerful tool with many capabilities, if interested you can learn about using pkill with wildcards and using pkill to kill all processes belonging to a specific user.
Know of another way to target a process by name to kill it from the command line? Let us know in the comments below!