109

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? Isn't this in the proc?

If not, then 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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
user489152
  • 1,193
  • 7
    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. –  Aug 04 '11 at 10:12
  • Could you clarify what this question was about in light of @LarsWirzenius's comment? Thanks! – Caleb Aug 04 '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 04 '11 at 14:51
  • 2
    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 'SO- stop being evil' Aug 04 '11 at 17:04
  • step 1 : top -u username step 2 : Press k to kill process accordingly – THaamarai Jun 20 '20 at 11:38

6 Answers6

142

Use pkill -U UID or pkill -u UID or username instead of UID. Sometimes skill -u USERNAME may work, another tool is killall -u USERNAME.

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

pkill allow both numberic and symbolic UIDs, effective and real http://man7.org/linux/man-pages/man1/pkill.1.html

pkill - ... signal processes based on name and other attributes

    -u, --euid euid,...
         Only match processes whose effective user ID is listed.
         Either the numerical or symbolical value may be used.
    -U, --uid uid,...
         Only match processes whose real user ID is listed.  Either the
         numerical or symbolical value may be used.

Man page of skill says is it allowed only to use username, not user id: http://man7.org/linux/man-pages/man1/skill.1.html

skill, snice ... These tools are obsolete and unportable. The command syntax is poorly defined. Consider using the killall, pkill

  -u, --user user
         The next expression is a username.

killall is not marked as outdated in Linux, but it also will not work with numberic UID; only username: http://man7.org/linux/man-pages/man1/killall.1.html

killall - kill processes by name

   -u, --user
         Kill only processes the specified user owns.  Command names
         are optional.

I think, any utility used to find process in Linux/Solaris style /proc (procfs) will use full list of processes (doing some readdir of /proc). I think, they will iterate over /proc digital subfolders and check every found process for match.

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

skill (procps & procps-ng) and killall (psmisc) tools both uses getpwnam library call to parse argument of -u option, and only username will be parsed. pkill (procps & procps-ng) uses both atol and getpwnam to parse -u/-U argument and allow both numeric and textual user specifier.

osgx
  • 1,770
  • 1
  • 11
  • 11
  • 2
    pkill is not obsolete. It may be unportable outside Linux, but the question was about Linux specifically. –  Aug 04 '11 at 10:11
  • 1
    to get the list of users use the one liner: getent passwd | awk -F: '{print $1}' – Anya Shenanigans Aug 04 '11 at 10:58
  • what about I give a command like: "kill -ju UID" from C system() call? –  Aug 04 '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 04 '11 at 12:10
  • @osgx: Sadly, kill is the only utility I have :( – user489152 Aug 04 '11 at 14:51
  • 1
    is it an embedded linux? you have no skill, pkill and killall? Even busybox embedded shell has pkill and killall. – osgx Aug 04 '11 at 15:01
  • 7
    killall -u USERNAME worked like charm – michalzuber Apr 23 '15 at 07:47
  • has skill changed now, as my skill expects a username with the -u flag, not the user ID: -u The next argument is a username. – Laurence Cope May 26 '17 at 14:30
  • @LaurenceCope, Thanks, I updated the answer. No, skill of procps-ng was not changed, only text username for skill -u username is allowed. Tool uses getpwnam libcall (it is defined only for user name, not for id) to parse -u argument for more than 5 years https://gitlab.com/procps-ng/procps/blame/master/skill.c case 'u': .... passwd_data = getpwnam(optarg); Same for skill in classic procps: http://procps.cvs.sourceforge.net/viewvc/procps/procps/skill.c?revision=1.15&view=markup. – osgx May 26 '17 at 15:02
  • skill -u username saved my day – RoboAlex Oct 30 '19 at 03:15
  • is there one that doesn't kill my ssh connection to the server I'm connected to? pkill -U user kills me and logs me out. – Charlie Parker May 03 '21 at 22:40
10

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);
}
  • This is the best solution. kill -1 avoids any race-conditions but please read the following article for additional information: https://lwn.net/Articles/754980/ – famzah Aug 17 '20 at 14:11
8

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

2
pgrep -U username|xargs kill -9
HalosGhost
  • 4,790
Alexander
  • 29
  • 1
0

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
slm
  • 369,824
-1
  1. Kill all process for user.
  2. Wait 2 seconds.
  3. Update username and home directory.
 killall -u user; sleep 2; usermod -l newuser -d /home/newuser -m user
Murphy
  • 2,649