Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill all processes for a given user

Tags:

posix

Is there a reliable way to kill all the processes of a given user? kill(-1, SIGKILL) as that user will work, unless a rogue process of that user kills the killing process first. The best I can find so far is to loop through system("ps -u") for that user and kill the processes that way, but that seems really hacky and inefficient.

EDIT: To clarify, I'm specifically asking for a POSIX-compatable solution. For some reason I thought tagging the question posix would put that in the title.

like image 488
Shea Levy Avatar asked Mar 16 '13 17:03

Shea Levy


People also ask

How do you kill all processes running by a user in Linux?

The easiest way to kill a bunch of processes altogether is through the killall command. The kill all command in Linux will first send a signal to every running daemon. If you do not specify any signal name, by default, it sends the SIGTERM.

How do I kill all processes in a process group?

If it is a process group you want to kill, just use the kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112 .

Which command can be used to kill all processes by using their name?

There are two commands used to kill a process: kill – Kill a process by ID. killall – Kill a process by name.


3 Answers

Just (temporarily) killed my Macbook with

killall -u pu -m .

where pu is my userid. Watch the dot at the end of the command.

Also try

pkill -u pu

or

ps -o pid -u pu | xargs kill -1
like image 113
uselpa Avatar answered Oct 06 '22 19:10

uselpa


Here is a one liner that does this, just replace username with the username you want to kill things for. Don't even think on putting root there!

pkill -9 -u `id -u username`

Note: if you want to be nice remove -9, but it will not kill all kinds of processes.

like image 31
sorin Avatar answered Oct 06 '22 18:10

sorin


On Debian LINUX, I use: ps -o pid= -u username | xargs sudo kill -9.

With -o pid= the ps header is supressed, and the output is only the pid list. As far as I know, Debian shell is POSIX compliant.

like image 8
jgrocha Avatar answered Oct 06 '22 19:10

jgrocha