2

I'm looking for a way in which I can force accounts left logged on to my Mac to auto logoff after a period of inactivity.

There is a setting built in to the operating system (Lion), that does this, but it applies to all users, and I only want to do this with certain accounts.

On Windows, I could use gpedit to force the users to use a screensaver that logs off the users. Is there something similar that I can do on Mac OS X Lion?

p.s. I'm the sole administrator on the system.

5
  • 1
    You could try going the other direction and use a program such as Caffeine to make the timeout not apply to some users, if these programs simulate user activity.
    – Daniel Beck
    Commented Jan 2, 2012 at 15:12
  • Oooh, that should do the trick nicely. If you repost as an answer I'll accept your solution.
    – user5024
    Commented Jan 2, 2012 at 17:03
  • Well, does it work? Haven't tried to use it with that option myself.
    – Daniel Beck
    Commented Jan 2, 2012 at 17:30
  • It seemed to work at first, but I've just found myself logged out due to inactivity. I think I may have had caffeine configured to only keep the system awake for a defined duration. I've now configured caffeine to work indefinitely, and am testing further, and will report back.
    – user5024
    Commented Jan 2, 2012 at 19:25
  • Unfortunately, it doesn't seem to work. I keep getting logged out despite caffeine being configured to keep the system in an active state. Many thanks for your suggestion though.
    – user5024
    Commented Jan 2, 2012 at 21:36

1 Answer 1

3

I've figured out a way of doing this, it's a bit of a hack using shell scripts, cron and sudo, but it seems to work pretty well.

First, create a shell script /bin/usertimeout owned by root, chmod it to 755, and paste the following content into the file

#!/bin/bash

# Timeout is the number of seconds a login session can be idle before it is
# automatically logged out.
timeout=3600

if [ $(stat -f %u /dev/console) == $UID ]
then
  if [ -e /tmp/backgroundUserLogout.$UID ]
  then
    rm /tmp/backgroundUserLogout.$UID
  fi
else
  if [ ! -e /tmp/backgroundUserLogout.$UID ]
  then
    touch /tmp/backgroundUserLogout.$UID
  else
    if [ $(( `date +%s` - `stat -f %m /tmp/backgroundUserLogout.$UID || printf 0` )) -ge $(( $timeout )) ]
    then
      rm /tmp/backgroundUserLogout.$UID
      sudo /sbin/killuser
    fi
  fi
fi

Next, create a file /sbin/killuser, owned by root, chmod it to 755 and paste in the following content

#!/bin/bash
#
# Logs out the user calling this script   

# Get the PID of the loginwindow process for the user executing this
pid=`ps -Axjc | grep ^$SUDO_USER | grep loginwindow | cut -c 14-20 | tr -d /\ /` 

# If the PID appears to be valid, kill the process
if [ $pid -gt 0 2>/dev/null ]
then
  kill -9 $pid
fi

Next add a crontab entry for each user that you want to be auto logged out. This would be a pain if you wanted all users to be affected, but in my case, I only require a small number of users to be logged out on idle.

# Crontab for user that has to be autologged out
* * * * * /bin/usertimeout

Note that the example above runs every minute, depending on the idle time you allow, you might want to increase this to a more suitable frequency (e.g. every 15 mins using */15 * * * * /bin/usertimeout)

Now a simple mod to the sudoers file using visudo and you're good to go.

%users          ALL=(ALL) NOPASSWD: /sbin/killuser
6
  • For info, the purpose of using sudo, is that kill -9 $pid alone isn't always enough to kill the loginwindow process. I also considered using kill -9 -1 which would normally kill all processes belonging to the user, but according to the man pages, when you run this with sudo, it instead kills all processes on the system.
    – user5024
    Commented Jan 3, 2012 at 23:52
  • Any changes I need to do here to make this work in Ubuntu?
    – DrorCohen
    Commented Dec 15, 2013 at 15:50
  • @DrorCohen I don't think it will, due to the use of stat -f %u /dev/console. It probably should be fairly trivial to fix for Ubuntu (assuming it doesn't work). If you can't get it working though, please create a new question.
    – user5024
    Commented Dec 15, 2013 at 19:58
  • can you please explain what $(stat -f %u /dev/console) == $UID is supposed to do? so I can convert it to ubuntu? thanks
    – DrorCohen
    Commented Dec 16, 2013 at 14:20
  • 1
    I managed to solve my problem - see my answer. thanks for your help.
    – DrorCohen
    Commented Dec 23, 2013 at 19:39

You must log in to answer this question.