Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.3: Kill another user logged in under Fast User Switching System
Fast User Switching has been a popular Panther feature in my lab where several students share one computer. However, ocassionally a user forgets to log out, and I (logged in as Administrator) have not figured out how to log them out gracefully.

While not elegant (for instance, it will not save unsaved changes in open files), here is one way to kill another user who is logged in under Fast User Switching. Assume the logged-in user is named boo. From the Terminal, type ps -axuc grep boo. In the results, look for the process called loginwindowassociated with the user boo. Note the process ID (the first column of the output) of loginwindow -- for purposes of this example, assume it was 1234. To kill the logged-in user, just type:
 % sudo kill -9 1234
The user will immediately disappear from the list of logged-in users. It's not elegant, but it works.
    •    
  • Currently 2.00 / 5
  You rated: 5 / 5 (7 votes cast)
 
[25,473 views]  

10.3: Kill another user logged in under Fast User Switching | 18 comments | Create New Account
Click here to return to the '10.3: Kill another user logged in under Fast User Switching' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3: Kill another user logged in under Fast User Switching
Authored by: nvdingo on Dec 01, '03 11:44:37AM

This is not tested, but under other *nixes, if you su to root, you can then su to the other username without their password (you are root after all)

in MacOS X, it would theoretically be possible to become the other user and run an applescript to kill their apps with saving, yes?

I am not an applescript guru. (IANAAG)
i am barely functional in applescript beyond the most basic script.

But shouldn't this be possible?

you can run them from a command line with osascript command.

wouldn't it be something like
get a list of all running applications (in the foreground)
for each app get a list of open documents
save each document into a special folder
quit app
then quit login for that user. (would telling the finder to logout work?)

that's my suggestion. I am open to hearing why this might not work. like i said, IANAAG.
and any feedback on this idea, would help advance mylimited AS knowledge



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: diamondsw on Dec 01, '03 11:59:53AM

If everyone implemented AppleScript properly, that would work fine. Sadly, very few applications actually properly support the language with identical syntax. Photoshop Elements comes to mind...



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: schwa on Dec 01, '03 12:11:13PM

Doesn't work unfortunately. I get told by osascript that '0:22: execution error: No user interaction allowed. (-1713)'.

SOL.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: Clint MacDonald on Dec 01, '03 04:27:45PM
in MacOS X, it would theoretically be possible to become the other user and run an applescript to kill their apps with saving, yes?

Actually, before submitting this hint, I tried writing an AppleScript to logout a user, then su to that user and do a

open -a logoutscript.scrpt

Unfortunately, all it did was log out my current user.



[ Reply to This | # ]
open doesn't work here!
Authored by: dom on Dec 01, '03 05:21:29PM
open
always opens as the current logged-in user. Instead, try this:
su other_acct_name
/Applications/logoutscript.scpt
or, in one line:
su other_acct_name -c /Applications/logoutscript.scpt


[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: tris on Dec 01, '03 09:25:36PM

Alternately....
A good shove down some steps or a thump with a stout stick will knock off most other users !



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: greggo on Dec 01, '03 11:55:48AM
You can use this line to narrow the search down...
ps -axuc grep * | awk '/loginw/{print $2, $1, $11}'
--> PID username loginwindow


[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: thrig on Dec 01, '03 12:09:38PM
% sudo kill -9 1234

No! Bad admin! Never use the KILL signal to kill by default. This is process assassination, and should never be done. Process 1234 might be the parent for any number of child processes that will end up in limbo because the KILL signal does not give the parent a chance to inform any children. Additionally, the KILL signal prevents a process from cleaning up any temporary files and shared memory segments, or shutting down socket connections. Instead, use the TERM signal by default, and work up to a KILL if the process in question proves resilient.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: Clint MacDonald on Dec 01, '03 04:36:54PM
Never use the KILL signal to kill by default. [...] Instead, use the TERM signal by default, and work up to a KILL if the process in question proves resilient.

Could you explain more about this, please? I am a command line newbie, and am unfamiliar with TERM. To be honest, I would prefer another way to logout another user as Admin, since kill has so many problems (as you point out).



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: 47ronin on Dec 01, '03 06:18:54PM

Yeah, you should NEVER use kill -9 unless you have to.. It's a last resort. the -9 flag forces the computer to kill the process without prejudice, and without cleaning up after itself. The TERM flag will attempt to close out any remainding resources that the process was using before trying to cleanly exit it.

Think of TERM as "Come out with your hands behind your head!"

Think of -9 as "Shoot the bastard where he stands!"



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: hamarkus on Dec 02, '03 10:00:59AM

I can only confirm this, I have managed to create kernel panics and stuck sub-processes (requiring a restart) with kill -9.

One thing one should always try before reverting to kill -9, is to apply the kill command several times. Killing e.g. pppd usually takes 4 to 5 kill attempts before it terminates.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: mikeygstl on Dec 02, '03 11:11:58AM

kill -15 `ps --sort -pid axo%p.%u | grep <USERNAME> | cut -d. -f1 -`

That should do it nicely. Sends SIGINT - not sigterm or sigkill.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: mikeygstl on Dec 02, '03 11:13:26AM

kill -15 `ps --sort -pid axo%p.%u | grep <USERNAME> | cut -d. -f1 -`

That should do it nicely. Sends SIGINT - not sigterm or sigkill.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: grumpy on Dec 01, '03 08:54:08PM

Using the TERM signal is no different to using the KILL signal if the application hasn't registered a special signal handler for the TERM signal to gracefully shutdown the process. Check out the sigaction manual page and you will see this is the case as the default action for TERM is "terminate the process", exactly the same as KILL.

Thus you are at the mercy of the application developer as to whether they intercept TERM and do anything special. You often see programs from the UNIX world which simply don't bother because the developers don't know any better. A decent program will catch both TERM and INT and shutdown nicely if they need to.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: amusingfool on Dec 01, '03 12:35:36PM

well, if you don't mind them not being able to save, you can use the following (bash) shell function

[code]
function killuser() {
sudo kill -9 `ps -auxg |grep $1 |grep -v grep |awk '{ print $2; }'`;
}
[/code]

the first argument (substituted for $1) should be the username of the user in question. If you really want to just get their loginwindow (not recommended), you can do:
[code]
function killuser() {
sudo kill -HUP `ps -auxgww |grep $1 |grep loginwindow |grep -v grep |awk '{print $2; }'`;
}
[/code]

Note that the ww in the ps options is very important, unless you use _very_ wide windows.



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: sfn on Dec 01, '03 01:53:13PM

Well, if you are sitting at the same machine just fire up Activity Monitor and force quit that user's app.


---
-sfn



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: sfn on Dec 01, '03 03:38:44PM

Oops, I see now. I misread the original post. AM won't kill a user's session just an app.

---
-sfn



[ Reply to This | # ]
10.3: Kill another user logged in under Fast User Switching
Authored by: acdha on Dec 01, '03 03:16:11PM
Alternately: killall -u username

[ Reply to This | # ]