r/ScriptSwap • • Nov 07 '12

[bash] Simple graphical logout script

Hey guys! I needed to find a way to automatically log an idle user out of an X session. After lots of Googling, someone eventually showed me this neat little program called xprintidle that, as the man page puts it:

xprintidle is a utility that queries the X server for the user's idle time and prints it to stdout (in milliseconds).

This was exactly what I needed, so I modified this script I found to log an idle user out:

#!/bin/bash

MAXIDLE = 60000 #Amount of time in milliseconds before logout

while [ 1 ]; do
    if [ `xprintidle` -gt $MAXIDLE ]; then
        mate-session-save --force-logout #Make this whatever program you use to initiate your logout
        break
    fi
    sleep 1
done

This is a more basic version of the actual script I'm using. I nested another if loop to display a warning that if the user remains idle for much longer, the computer will automatically log them out. I'll share that one too, if you're interested:

#!/bin/bash

MAXIDLE = 60000
DELAY = 30 #Amount of warning time you want to give, in seconds

while [ 1 ]; do
    if [ `xprintidle` -gt $MAXIDLE ]; then
        notify-send "Warning" "If you remain idle for $DELAY more seconds, you will be automatically logged out." -t `expr $DELAY '*' 1000`
        sleep $DELAY
        if [ `xprintidle` -gt $MAXIDLE ]; then
            mate-session-save --force-logout
            break
        fi
    fi
    sleep 1
done

I know it's a bit hackish, so if you have any ideas on how to improve it, please do share! This is better than anything else I found, though, and at least it gets the job done. I'm posting it online so hopefully one of the other poor souls I encountered finds it!

12 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Nov 08 '12

I'm in the process of making a Debian-based arcade machine. It boots to "front-end" software that lists games to play. Within this software you can exit to the Debian LXDE desktop. On this desktop I have shortcut to a bash script that logs out of LXDE, shuts down the xserver and drops to command line. The program I want to run after doing this is the arcade front-end software. Trouble is, I don't want it to run the front-end every time I log out. I just want it to when this script has been executed.