(Updated: this post was rewritten from an older post. That original has generated a lot of comments over the years; please read the whole thing before jumping to add your piece)
It is sometimes necessary to kill all the processes owned by a particular user.
On a modern Linux box or Mac OS X, we'd just do "killall -u username" and be done with it, but that "killall" may not exist on other Unix platforms and if it does exist, it may be an entirely different command with unexpected results.
A common suggestion is something like this:
kill -9 `ps -u <username> -o "pid="`
It all comes down to "a little knowledge is a dangerous thing". If all you know is one variant (like Linux), it can be very dangerous to assume that what you know will work elsewhere. Most of it will, of course, but the Devil is in the details.
Something I should have thought of but did not is to "su username" (if you are root, you don't need their password) and then issue "kill -9 -1" (although again you might want to start with -15 if there are processes that could benefit from a chance to exit gracefully).
The "ps -u" by itself gives you the chance to pick and choose which of the user's processes you want to kill, though. When -j is added ("kill -ju username"), you might be able to pick out a group of processes that can be killed without affecting things you don't want to kill. An example:
$ ps -ju apl | sort -k4 | tail USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND apl 3041 220 3041 53a83a8 1 S ?? 0:02.44 /Library/.. apl 3942 220 3942 53a83a8 1 S ?? 0:39.19 /Applications/Text.. apl 4471 291 4471 8696998 1 S+ s000 0:00.00 -bash apl 4472 4471 4471 8696998 1 S+ s000 0:00.94 ssh [email protected] apl 5472 515 5472 8696ad0 1 S+ s001 0:00.00 -bash apl 5473 5472 5472 8696ad0 1 S+ s001 0:00.07 ssh [email protected] root 5507 969 5507 8696248 3 R+ s003 0:00.01 ps -ju apl apl 5508 969 5507 8696248 3 S+ s003 0:00.00 sort -k4 apl 5509 969 5507 8696248 3 S+ s003 0:00.00 tail
(I moved the header back to the top to make it easier for you to see it). This listing shows processes belonging to "apl" and sorted by "pgid" - the process group id. If I wanted to kill of everything in 5507, I could do "kill -9 -5507" (note the minus sign in front of 5507) - though in this particular case "kill -9 5507" would do the same thing.
A buggy version of prngd caused an interesing bug on an old SCO system: "Dying processes (inetd, cron, syslogd, sshd)".
There's plenty of other stuff here about "kill" as it applies to Unix systems.
Note: as your shell may implement kill itself, you want "/bin/kill" if you want your real Unix kill - there can be differences! That "KTFM (Killing the Fine Manual" article also shows some of that. To read about how "bash" implements "kill", do "man builtin" (or "info bulitin") and search for "kill".
If you found something useful today, please consider a small donation.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2013-08-17 Tony Lawrence
There are only two hard problems in computer science: cache invalidation and naming things. (Phil Karlton)