How to kill all processes run by a user on Linux

Last updated on September 15, 2020 by Dan Nanni

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.

Method One: grep

The first method is to feed kill command with a list of process IDs generated by ps command.

$ ps -ef | grep xmodulo | awk '{ print $2 }' | sudo xargs kill -9

Method Two: pgrep

A more convenient way to look up processes based on user name is to use pgrep.

$ pgrep -u xmodulo | sudo xargs kill -9

Method Three: pkill

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.

$ sudo pkill -u xmodulo

Method Four: killall

killall is very similar to pkill in terms of killing processes by user name.

$ sudo killall -u xmodulo

Method Five: slay

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 apt-get install slay
$ 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!

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean