When a background process hangs or consumes 100% of your CPU on a Linux server, the standard operating procedure is to kill it. Typically, administrators use the kill command, which requires you to first look up the exact numeric Process ID (PID) using tools like top or ps. If you are dealing with a cluster of rogue processes (for example, if five different PHP workers have all frozen simultaneously), hunting down and typing out five different PIDs is tedious and prone to typos. To speed up your workflow, you can use the pkill command, which allows you to terminate processes instantly using only their human-readable name.
Basic Usage of pkill
The pkill command searches the system’s process table for any running program whose name matches the string you provide. Once it finds a match, it immediately sends a termination signal.
- Open your Linux terminal. (Note: You usually need
sudoprivileges to kill system-level services like web servers). - Type the command:
sudo pkill nginx - Press Enter.
Unlike the standard kill command, pkill is ruthless. If there are eight different Nginx worker processes running, this single command will instantly terminate all eight of them simultaneously, because they all match the string “nginx”.
The Danger of Broad Matching
You must be incredibly careful when using pkill, because it uses partial string matching by default. It does not look for an exact word match.
For example, if you want to kill a hung script named java_updater, and you type pkill java, you will successfully kill your updater script. However, because pkill uses partial matching, it will also immediately kill your massive Minecraft server and your enterprise Tomcat web server, because both of those processes contain the word “java” in their names.
To prevent this disaster and force pkill to only terminate processes that match the exact name perfectly, you must use the -x (exact) flag.
sudo pkill -x java_updater
Killing Processes Owned by a Specific User
If a specific user on a shared server is running a script that is consuming too much RAM, you might want to kick them off the system. Instead of killing programs by name, you can tell pkill to terminate everything owned by that user by utilizing the -u (user) flag.
sudo pkill -u john_doe
This command will instantly terminate every single bash shell, ssh connection, and background script running under the john_doe user account, effectively logging them out and reclaiming your server’s resources.