Not sure how it was possible that I have not done this before but I recently realized I needed to forcibly remove a user from a login session on a remote Linux system and didn’t have a better idea than simply killing off all their individual system processes one at a time. Thankfully, Linux provides a much more useful way of dealing with kicking users from terminal sessions (and thereby shutting down their entire process tree as well.)
$who -u
Will give you a list of user sessions based on which terminal they are logged into. This includes X sessions, virtual terminals, remote sessions, and any text mode logins. The output should looks something like this:
bobby :0 2011-04-21 20:01 ? 12122
bobby pts/0 2011-04-21 20:01 . 12405 (:0)
bobby pts/1 2011-04-21 20:01 02:10 12322 (:0)
root pts/2 2011-04-21 22:19 . 13887 (10.0.0.101)
You can then kill the session login by looking at the last column and killing that process ID. In the example above you can see there are two virtual terminals (i.e. the pts/X sessions), a remote session (the ssh session I am remotely accessing the machine on from host 10.0.0.101), and a single local login on terminal session :0. Because the terminal session must be responsible for starting the virtual terminals, you can simply kill the process 12122 force a logout of all three sessions.
$kill 12122
Entirely too easy. If you would like to be kind (I am NOT) and actually warn your users that you are bout to kick them off, you can send them a system message using the standard Unix wall command. If you type wall you will get an open text area to type your message (end the message by clicking Ctrl+d) or you can pipe a message to standard input like so:
$echo “My name is Inigo Montoya. You killed my father. Prepare to die.” |wall
Wall will send a system message to every terminal session that allows messages (if you are root, that means everybody.)