Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to kill all running processes of a particular user from either a shell script or native code on a linux system.

Do I have to read the /proc directory and look for these?

Any ideas? Is there a dynamic mapping of the pids under UIDs in linux? Isnt this in the proc?

If no where is this list maintained. Should I read from it? Also where is the static list of all UIDs in the system so I can validate this this user exists and then proceed to kill all processes running under it?

share|improve this question

migrated from stackoverflow.com Aug 4 '11 at 13:21

This question came from our site for professional and enthusiast programmers.

Do you want a tool to do this (pkill, slay, others exist), or do you want to write it yourself? If the former, the superuser stack exchange site is probably better. If the latter, scanning /proc and making a note for all processes by a specific user is the way to go. The source code for the pkill utility would, for example, show how to do that. – Lars Wirzenius Aug 4 '11 at 10:12
Could you clarify what this question was about in light of @LarsWirzenius's comment? Thanks! – Caleb Aug 4 '11 at 14:03
@Caleb: I wanted to kill processes by reading the /proc as I didn't know of any tool doing it. Also now I find that other than "kill", "pkill", "skill" etc are not available on my system. In that case I guess I have to look at shell script alternatives to read the /proc and figure out a way to get processes under one user. Any ideas? – user489152 Aug 4 '11 at 14:51
Regarding “the static list of all UIDs … so I can validate this this user exists”: there is no such thing as validating a user ID. User names come from a database, but user IDs are whatever a process running setuid() chooses. – Gilles Aug 4 '11 at 17:04
up vote 9 down vote accepted

Use either skill -u UID or pkill -u UID.

Skill was a linux-specific and is now outdated, and pkill is more portable (Linux, Solaris, BSD)

I think, both utilites have no access to any list other than full list of processes (or readdir of /proc). I think, they itterate over /proc and check every process for match.

PS: Another tool is killall .

To get list of users, use getpwent (it will get one user per call).

share|improve this answer
pkill is not obsolete. It may be unportable outside Linux, but the question was about Linux specifically. – Lars Wirzenius Aug 4 '11 at 10:11
to get the list of users use the one liner: getent passwd | awk -F: '{print $1}' – Petesh Aug 4 '11 at 10:58
what about I give a command like: "kill -ju UID" from C system() call? – user489152 Aug 4 '11 at 12:07
is it an embedded linux? you have no skill, pkill and killall? Even busybox embedded shell has pkill and killall. – osgx Aug 4 '11 at 15:01
killall -u USERNAME worked like charm – michalzuber Apr 23 '15 at 7:47

If the pkill function is unavailable on your UNIX / Linux distribution you could run the following command as the root user:

ps -ef | grep username | grep -v grep | awk '{print $2}' | xargs kill

where username is the user who's processes you want to delete

share|improve this answer
or just the pkill -U username. – osgx Aug 4 '11 at 12:49

If you pass -1 as the process ID argument to either the kill shell command or the kill C function, then the signal is sent to all the processes it can reach, which in practice means all the processes of the user running the kill command or syscall.

su -c 'kill -TERM -1' bob

In C (error checking omitted):

if (fork() == 0) {
    signal(SIGTERM, SIG_DFL);
    kill(-1, SIGTERM);
share|improve this answer

This has worked nicely for me. You can find all the pid's of the processes by username by doing ps U <username> and go from there. Try this:

ps U <username> | cut -d " " -f 1 | xargs kill
share|improve this answer
pgrep -U username|xargs kill -9
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.