3

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).

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • 3
    What do you mean by user inactivity? No keyboard/mouse input? What happens if you are running a process at the moment (like downloading a large file)? – Julien Bourdon Nov 25 '12 at 11:32
  • Please define the inactivity properly. – lashgar Nov 25 '12 at 12:41
  • Yes, sorry... I did just update the question, better specifying what I mean by user inactivity – MarcoS Nov 25 '12 at 14:05
  • I assume you want an command line tool, but it should still depend on X. Because without X there wouldn't be anything like mouse or keyboard input which could get tracked. – miho Nov 25 '12 at 14:12

2 Answers2

2

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.

miho
  • 11,765
  • 7
  • 42
  • 85
  • But, how to you run it in 'unattended' mode ? (from crontab?) – MarcoS Nov 25 '12 at 14:17
  • 1
    You just need it to launched once, then it will be there the whole session. This can be done in the init.d or with an @reboot based crontab event. – miho Nov 25 '12 at 14:20
1

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.

ddoxey
  • 2,013
  • 1
  • 18
  • 25
  • Il like your answer. But, on my system (Ubuntu 12.10) the time for my screensaver process ("gnome-screensaver") is always 00:00:04 since 15 minutes I'm testing it... (the system is on since 10 hours...) – MarcoS Nov 25 '12 at 16:14
  • I find that my screensaver always says that it's been running for 16 seconds. I'm speculating that the xscreensaver process is active and polls for activity at that interval. – ddoxey Nov 25 '12 at 16:21
  • Yes, it looks like it's the CPU time, not the clock time... :-( – MarcoS Nov 25 '12 at 16:24
  • On [[ vs. [ in bash conditionals, I've never been all that certain about the difference. I found this: http://serverfault.com/questions/52034/what-is-the-difference-between-double-and-single-square-brackets-in-bash – ddoxey Nov 25 '12 at 16:27
  • This solution is a bust without a correct way to determine clock time for the process. Digging through man for top and ps and I'm not coming up with anything. – ddoxey Nov 25 '12 at 16:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20077/discussion-between-marcos-and-ddoxey) – MarcoS Nov 25 '12 at 17:57