Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to execute a command such as

 notify-send 'a'

if my Linux machine has been idle for 5 minutes.

By idle, I mean the same thing a screen saver that gets activated would use to define "idle".

share|improve this question
    
OK, what have you tried? What did google say? Have you attempted to find out how the screensaver detects this? Please take the time to search before posting questions here and read through How to ask a good question. –  terdon Aug 29 '13 at 18:06
    
I only found a program xautolock but there are no examples –  user249704 Aug 29 '13 at 18:14
add comment

1 Answer

I use a program called xprintidle to find out the X idle time, which I'm strongly guessing uses the same data source as screensavers. xprintidle doesn't really seem to have an upstream anymore, but the Debian package is alive and well.

It is a very simple application: it returns the amount of milliseconds since last X interaction:

$ sleep 1 && xprintidle
940
$ sleep 5 && xprintidle
4916
$ sleep 10 && xprintidle
9932

(note: due to the underlying system, it will consistently give a value in ms slightly lower than the "actual" idle time).

You can use this to create a script that runs a certain sequence after five minutes of idle time via e.g.:

#!/bin/sh

# Wanted trigger timeout in milliseconds.
IDLE_TIME=$((5*60*1000))

# Sequence to execute when timeout triggers.
trigger_cmd() {
    echo "Triggered action $(date)"
}

sleep_time=$IDLE_TIME
triggered=false

# ceil() instead of floor()
while sleep $(((sleep_time+999)/1000)); do
    idle=$(xprintidle)
    if [ $idle -ge $IDLE_TIME ]; then
        if ! $triggered; then
            trigger_cmd
            triggered=true
            sleep_time=$IDLE_TIME
        fi
    else
        triggered=false
        # Give 100 ms buffer to avoid frantic loops shortly before triggers.
        sleep_time=$((IDLE_TIME-idle+100))
    fi
done

The 100 ms offset is because of the earlier noted quirk that xprintidle will always return a time slightly lower than the "actual" idle time when executed like this. It will work without this offset, and will then be more accurate to a tenth of a second, but it will trigger the xprintidle check frantically during the last milliseconds before an interval end. Not a performance hog in any way, but I would find that inelegant.

I have used a similar approach in a Perl script (an irssi plugin) for quite some time, but the above was just written and has not really been tested except for a few trial runs during writing.

Try it by running it in a terminal within X. I recommend setting the timeout to e.g. 5000 ms for testing, and adding set -x directly below #!/bin/sh to get informative output to see how it works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.