Pkill Command - Kill Processes by Name

Learn how to use the pkill command to kill processes by name. This guide covers killing processes by full name and partial name with examples.

Pkill Command - Kill Processes by Name

Understanding the Pkill Command

The pkill command in Linux is a powerful utility that allows you to send signals to processes based on their names or other attributes. Unlike the traditional kill command, which requires a process ID (PID), pkill simplifies the process of terminating multiple processes at once by using pattern matching on process names.

Killing Processes by Full Process Name

To terminate a process using its exact name, you can directly provide the process name as an argument to pkill. This is useful when you know the precise name of the process you want to stop.

# To kill a process using its full process name:
pkill <process-name>

For example, to kill all processes named nginx, you would use:

pkill nginx

Killing Processes by Partial Name

The -f option with pkill enables you to match the pattern against the full command line, not just the process name. This is incredibly useful for killing processes that might have arguments or are part of a script.

# To kill a process by its partial name or command line string:
pkill -f <string>

For instance, if you want to kill any process containing the string my_script.py in its command line, you would execute:

pkill -f my_script.py

This will terminate any process whose command line includes my_script.py, regardless of its exact executable name.

Important Considerations

Always be cautious when using pkill, especially with the -f option, as it can terminate multiple processes if the pattern matches more than one. It's recommended to first use pgrep with the same pattern to see which processes would be affected before actually killing them.

# Example: Check which processes match the pattern before killing
pgrep -f <string>
pkill -f <string>

Further Resources