Kill All Processes Belonging to a User with pkill

Feb 18, 2013 - 1 Comment

Kill all processes belonging to a user

Activity Monitor and the traditional ‘kill’ command line tool are able to handle most process termination needs, but if you’ve ever needed to target and kill all processes belonging to a single user account, you know it can be a frustrating task. Though Activity Monitor allows you to sort “Other User Processes” and select multiple processes, it does not allow you to terminate multiple processes at the same time. Similarly, the standard kill and killall commands are generally aimed at specific processes, and not at every single task belonging to a specific user account. This is where the ‘pkill’ command comes in, which makes it simple to instantly kill every single process belonging to any user via the terminal.

The basic syntax for using pkill to kill all of a users processes is as follows:

pkill -u username

Verify that all processes belonging to that user have been terminated by using the -u flag on the ps command:

ps -u username

Assuming everything went as intended, you will find a blank list reported back.

pkill is not case sensitive, meaning a username of “TestUser” is going to be identified the same as “testuser”.

If you’re going to try this out on yourself, it would be best to use fast user switching to initiate a new login with another user account, or to use the ssh server and perform it on another local Mac. Using pkill on your own active username will cause all processes to terminate, some of which refresh instantly, but many background processes will not start again automatically. This leads to all sorts of peculiar behavior, and depending on what you have running don’t be terribly surprised to find the OS will become so unusable that you’ll either need to log out and in again, or even reboot if the active user account targeted by pkill was either root or administrative level.

The pkill command is a bit of a blowtorch when pointed at usernames and can be thought of as a way to essentially forcibly quit everything belonging to a logged in user, but that can also make it a very powerful tool for troubleshooting and when dealing with defunct or zombie processes that have stayed intact despite a user logging out.

We’ve discussed the pkill command before to kill processes with wildcards and specific apps/processes belonging to a single user, and though it’s a recent addition to the Mac since OS X Mountain Lion, it’s been around in the Linux world for quite some time.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

One Comment

» Comments RSS Feed

  1. Bruno Gama says:

    pkill is awesome.

    i use this one a lot.
    pkill -9 -fi chrom # it will do a kill -9 in all proccess that matches the string “chrom” the -f does a search for the matchin string in the proccess (mutch like a ps aux | grep) and the -i matches strings in insensitive case.

    to kill -9 more then 1 proccess i’ve created a pretty simple, but useful, function

    k() { for i in $*; do pkill -9 -fi $i; done }

    now I can kill more than one stuff at once.

    k chrom slate dropbo tvsho shortc
    # in my machine it will matches the proccess “Google Chrome”, Slate, Dropbox, TVShows, Shortcat

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates