on Technology

Unix/Linux: Finding and Killing Processes by User

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):

You can also filter the list of results by appending a full (or partial) process name to the command:

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:

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:

As always, check the man pages for pgrep and pkill for more information and switch options.

Write a Comment

Comment