Logout previously logged in user at login

DeployAdam
New Contributor III

In a lab environment we'd like to log out a current logged in user while fast user switching is enabled. Can anyone come up with a script to accomplish this? The previously logged in user can be anyone.

When logging in all previous sessions must be stopped immediately and not with a certain amount of inactivity.

Thanx in advance

8 REPLIES 8

thoule
Valued Contributor II

What you want to do to accomplish this task would be to 'ps auxw |grep loginwindow'. Then loop through and kill any process not owned by the current user.

sean
Valued Contributor

So you want Fast User Switching, without Fast User Switching. Isn't that just Logout then Log in or a I missing something?

DeployAdam
New Contributor III

Totally right, we'd like to see in the right uppercorner the user who is logged in at the current time but we only like to have 1 user logged in at a time. Does this make sense?

sean
Valued Contributor

Probably easier to write your own. This may help.

Menu Bar App

Alternatively, you could look at @thoule suggestion, just be careful trying to work out who the current console user is:

Jamf 19308

thoule
Valued Contributor II

I haven't tested this as I don't have fast user switching on, but it's a starting point. Run it every 10 mins or so as root...

#!/bin/sh                                                                                                                  

curName=`ls -l /dev/console | awk '{ print $3 }'`
userList=`ps auxw |grep -i loginwindow|awk '{print $2}'` #i know, awk after grep...
for aUser in $userList; do
    if [ "$curName" == "$aUser" ]; then
        echo "don't kill yourself, man!"
    else
        echo "removing user $aUser"
        kill $aUser
done

EDIT: as per @sean below, be very careful and test this method extensively. Not sure how it will work.

sean
Valued Contributor

As per the jamf thread I mentioned, be very careful with this method. The owner of /dev/console may not be who you expect it to be. Working out "Current User" may not work as expected when multiple users are logged in.

Look
Valued Contributor III
last -t console -1

I haven't tested it in a login policy context but this should give you the last user to log in. A background user unlocking the machine doesn't update it, however as the whole purpose of the excercise is not to have any locked background users that shouldn't matter.

sean
Valued Contributor

Background user is unclear? Are you talking ssh as this won't have any effect on the console? Remote console will only change console on login, but probably not on an unlock.

I have a machine from testing previously that still registers me as the owner of console and I'm not even an active user any more.

"user1" logged in before the remote console user "testuser" logged in with a remote GUI login session and subsequently logged out. Since there was no login activity since, ownership of console has not updated and "user1" won't be the top of the list in the 'last' list of users.

$ who
user1 console  Mar 23 10:25 

$ last -t console  -1
testuser   console                   Tue Mar 29 11:02 - 13:47

$ stat -f%Su /dev/console 
testuser

I would suggest disable Fast User Switching and use the link above to create a Menu Item that shows Current User, which (with all other methods disabled) will mean you can query the console.