Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 Jan 2 '12 at 15:12
    
Oooh, that should do the trick nicely. If you repost as an answer I'll accept your solution. – Bryan Jan 2 '12 at 17:03
    
Well, does it work? Haven't tried to use it with that option myself. – Daniel Beck Jan 2 '12 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. – Bryan Jan 2 '12 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. – Bryan Jan 2 '12 at 21:36
up vote 2 down vote accepted

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
share|improve this answer
    
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. – Bryan Jan 3 '12 at 23:52
    
Any changes I need to do here to make this work in Ubuntu? – DrorCohen Dec 15 '13 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. – Bryan Dec 15 '13 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 Dec 16 '13 at 14:20
    
OK. managed to get a copy of the stat man for mac... The problem I face now is that /dev/console in Ubuntu belongs to root... – DrorCohen Dec 17 '13 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .