If you are a system administrator, there are situations where you want to kill all processes belonging to a specific user, for example because the user is being removed from the system, or the user is forking malicious processes or runaway daemons, etc. It will be cumbersome to kill individual processes of the user one by one manually.
In this tutorial, I will show how to kill all running processes launched by a user at once. Here, I will demonstrate several commands that kill all processes owned by Linux user "xmodulo".
The first method is to feed kill command with a list of process IDs generated by ps command.
A more convenient way to look up processes based on user name is to use pgrep.
pkill can streamlines the whole process of looking up processes by user, and sending them signals. A default signal sent by pkill is SIGTERM. Using pkill, you can kill all processes by owner easily.
killall is very similar to pkill in terms of killing processes by user name.
A command line tool whose sole purpose is to kill processes by owner name is slay. It provides "clean kill" mode, where matched processes are first sent SIGTERM signal, and those that haven't been terminated after 10 seconds are killed with SIGKILL signal. slay is available on Ubuntu or Debian. To install slay and use it:
$ sudo slay xmodulo
Precaution: When using slay, pay particular attention to sudo or run it as root. If you by accident attempt to slay another user as non-root user, it will of course not work, and more importantly, slay will kill your own processes!
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!