(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="`
That should work on most Unix variants, though really you shouldn't
start off with a -9 and you also need to watch out for variants on how "ps" works.
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@example.com
apl 5472 515 5472 8696ad0 1 S+ s001 0:00.00 -bash
apl 5473 5472 5472 8696ad0 1 S+ s001 0:00.07 ssh firstname.lastname@example.org
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: "prngd killed all processes".
See KTFM (Killing the Fine Manual) and Background tasks - Why? for more interesting things about process groups.
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 this page was useful to you, please help others find it:
More Articles by Tony Lawrence
- Find me on Google+
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Jump to Comments
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.