Question

I need to detect user inactivity on my Linux system, to poweroff it (quite headless wife, and quite expensive electric bills... :-).

I need to schedule the script (in crontab), so no X-depending tool will work, I suppose (no $DISPLAY available).

Any thoughts?

UPDATE

For "user inactivity" I mean user input inactivity (mouse and keyboard).

Was it helpful?

Solution

Xautolock may the right tool for you. It allows you to specify a amount of minutes of inactivity after which a command should get triggered.

OTHER TIPS

You might consider checking how long the screen saver has been running.

#!/bin/bash

screensaver="atlantis"

t=$(
    # check for the screensaver
    ps h -o start -C $screensaver          |\
    # hh:mm:ss -> seconds
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null  | sort -n | tail -1
)

if [ "$t" == "" ]
then
    exit 0
fi

n=$(
    date "+%T"                             |\
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null
)

runtime=$(( $n - $t ))

if [ $runtime -gt 3600 ] || [ $runtime -lt 0 ]
then
    echo shutdown -h now 
fi

Using the time value requires subtracting now from then to get the run time. Also, in my case, the screensaver program which appears in the process table will vary depending on which screensaver is selected. So, the above program assumes that 'atlantis' is the current screen saver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top