Auto Force Log Out

Caist
New Contributor

I am looking for a way to auto log out a user after being idle for 20 minutes. I need this to be a forced log out, so even if there is something open, it still needs to initiate the log out.

Thanks!

3 REPLIES 3

bpavlov
Honored Contributor

Here's the part where it logs out the user.

#!/bin/sh

#Determine PID for WindowServer to log out user
WindowServer="$(ps -axc | grep WindowServer | awk '{print $1}')"

#Force user log out
kill -HUP $WindowServer

exit 0

In terms of calculating idle time, not sure what the best way would be to accomplish that. Hopefully someone can come up with some ideas. A quick google search found me this: http://hints.macworld.com/article.php?story=20040330161158532

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

which you can test by doing this:

sleep 5; echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

mm2270
Legendary Contributor III

I'm not a big fan of killing the loginwindow process. It works, no doubt, but seems like a less than graceful approach to me. If there wasn't a slightly more graceful way I would say to use that, but you can also try the following.

#!/bin/sh

/usr/bin/osascript << EOF
ignoring application responses
tell application "loginwindow" to «event aevtlogo»
end ignoring
EOF

This does an immediate logout and force closes any open applications, even with unsaved documents open. The only time this may not work is if an application is hung for some reason and needs to be forced quit. In that case, this may not work, but otherwise it should.

For reference, the «event aevtrlgo» part is telling the loginwindow process to run an event. aevt is short for "Apple Event", and rlgo actually means "really log out" Wait, really? Yes, really! :)

Edit: OK, hmm. Just thought I'd test this again under Yosemite and seems like something might have changed. under Mavericks this used to work to force logout, but now it gets stuck as applications with unsaved work stop the process. Not sure what happened between those releases, but at least in my case its not working anymore under 10.10. Ugh. OK, never mind then, I guess killing the loginwindow actually IS the only way now. Dumb.

bentoms
Release Candidate Programs Tester

In regards to timing, a LaunchAgent that runs after 20 minutes & triggers the script might work.