Kill All Processes Belonging to a User with pkill
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.