Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
5  
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
1  
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
add comment

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

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

3 Answers

up vote 6 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
1  
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
1  
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
    
Yes, you can. Not a kill, but pkill -u UID -##, where UID is number user id and ## is signal number. –  osgx Aug 4 '11 at 12:10
1  
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
show 1 more comment

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
add comment

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) {
    setuid(uid);
    signal(SIGTERM, SIG_DFL);
    kill(-1, SIGTERM);
}
share|improve this answer
add comment

Your Answer

 
discard

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.