Kill Process by Name in Ubuntu - Command Line Guide
September 25, 2006
Topics:Linux & macOS TerminalLinux

Terminating Processes by Name in Linux
When the need arises to stop a running process, knowing its name provides several methods for termination. This is particularly useful when the process ID (PID) is unknown.
Methods for Process Termination
Several commands can be employed to kill a process using its name. Let's consider the example of a process named irssi to illustrate these techniques.
kill $(pgrep irssi): This command utilizes pgrep to find the PID(s) associated with the process name and then sends a termination signal using kill.killall -v irssi: killall directly terminates processes matching the specified name. The -v flag provides verbose output, confirming the action.pkill irssi: Similar to killall, pkill sends a signal to processes based on their name.kill \`ps -ef | grep irssi | grep -v grep | awk '{print $2}'\`: This approach combines several commands. ps -ef lists all processes, grep irssi filters for the target process, grep -v grep excludes the grep process itself, and awk '{print $2}' extracts the PID. The kill command then terminates the process using the extracted PID.
Each of these commands offers a distinct way to achieve the same outcome: stopping a process when only its name is known.
Use Cases in Scripting
These methods are especially valuable within shell scripts. In automated tasks, the PID of a process may not be readily available.
Therefore, the ability to terminate a process by name allows for reliable restarting or killing of processes as needed, enhancing the robustness of your scripts.