Posted on 03-29-2016 07:22 AM
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
Posted on 03-29-2016 07:25 AM
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.
Posted on 03-29-2016 07:32 AM
So you want Fast User Switching, without Fast User Switching. Isn't that just Logout then Log in or a I missing something?
Posted on 03-29-2016 07:37 AM
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?
Posted on 03-29-2016 07:43 AM
Probably easier to write your own. This may help.
Alternatively, you could look at @thoule suggestion, just be careful trying to work out who the current console user is:
Posted on 03-29-2016 08:04 AM
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.
Posted on 03-29-2016 10:38 AM
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.
Posted on 03-29-2016 02:34 PM
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.
Posted on 03-30-2016 06:31 AM
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.