If you’ve ever run ps aux | grep user to list processes and hunt for process IDs, you’ll be happy to know there is a simpler alternative. Both finding and killing processes owned by a particular user is made simple using the handy pgrep and pkill utilities.
Listing Processes with pgrep
Listing all the processes owned by the user raam can be done like this (the -l switch causes the output to include the process name):
$ pgrep -l -u raam 9614 screen 9628 bash 9644 irssi 16165 bash 16297 rtorrent 19462 ssh 19515 bash 19526 ssh 20964 sshd
You can also filter the list of results by appending a full (or partial) process name to the command:
$ pgrep -l -u raam bash 9628 bash 16165 bash 19515 bash
Killing Processes with pkill
The pkill command does basically the same thing as pgrep, except it kills the processes instead of listing them. This is useful if you have a user with several dead processes, or if you were deleting a user and you wanted to kill any running processes first.
Killing all the processes owned by the user raam looks like this:
$ pkill -u raam
And once again, if you only wanted to kill all the bash processes owned by raam, you would append the process name to the command:
$ pkill -u raam bash
As always, check the man pages for pgrep and pkill for more information and switch options.

0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.