How do I auto log off users from Ubuntu

4

1

Since my Ubuntu station was turned into Multi user Station (kids growing up) I need a way to log off automatically accounts after inactivity period (total log off not just lock screen). How can I achieve this in Ubuntu? I tried using autolog but inserting the line:

name=*          idle=15 grace=60

doesn't seem to do the work. Another option is to transform this answer into Ubuntu compatible but I need help in what needs to change...

DrorCohen

Posted 2013-12-15T15:59:36.810

Reputation: 234

Answers

2

This is how I solved my problem (this will logout an idle user after at least 30 minutes of idle time):

First create a file and put this code in it (you might need to install xprintidle before you start):

#!/bin/bash
# Written by cz0 2010, adapted by dror 2013 
# Distributed under the terms of the GNU General Public License v2 

HALFHOUR=1800000
IDLETIME=`xprintidle`
QDBUS="/usr/bin/qdbus" 

if [ $IDLETIME -gt $HALFHOUR ]
then 
    logger timeout of $HALFHOUR expired. idle is $IDLETIME
    KDEPID=$(ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}') 
    KDEUSER=$(ps u $KDEPID | grep 'startkde' | awk '{print $1}') 

# If the DBUS_SESSION_BUS_ADDRESS environment variable is not already set correctly 
# then set it by finding the environment file for the startkde process in proc and 
# parsing it to get get the correct setting. 

    if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then 
        ENVIRON_FILE=/proc/$(ps h --ppid $KDEPID -o pid | awk '{print $1}')/environ 
        CURRENT_DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS $ENVIRON_FILE | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//') 
        export DBUS_SESSION_BUS_ADDRESS=$CURRENT_DBUS_SESSION_BUS_ADDRESS
    fi 
    $QDBUS org.kde.ksmserver /KSMServer logout 1 0 2 
else
    logger timeout is $HALFHOUR not expired $IDLETIME 
fi

Now cp it to /bin/

sudo cp myfile /bin/logoutonidle

Next make it executavble

sudo chmod +x /bin/logoutonidle

Next run per the user you want to logout automatically (if needed then run it on every user):

crontab -e

and add to it the following line (this will make cron check every 15 minutes so worst case is that user will be logged out after ~45 idle time):

*/15 * * * * export DISPLAY=:0 && /bin/logoutonidle 2>&1

I would like to thank the people in these threads Bryan and Johanns for pointing me in the right direction.

DrorCohen

Posted 2013-12-15T15:59:36.810

Reputation: 234

1Supplement to avoid error reporting: CODI="couldn't open display" if [ "$IDLETIME" -eq "$CODI" ];then exit fi – Klaus-Peter – 2017-04-16T06:04:00.490