3

I would like to write a simple script that should work as a daemon. The only thing this daemon has to do, is to check if for a given period neither a key has been hit, nor a mouse moved, nor a touchpad touched, and if it is so, it should switch off the pc.

I realize it is quite a simple program, because it consists of only one condition and one action, but I am clueless how to check this condition of idleness.

Faheem Mitha
  • 35,108

1 Answers1

0

You could do something like this (untested, and would only work on Linux):

#!/bin/zsh
TIMEOUT=6000 # hundredths of seconds; in this case, 10 minutes
zmodload zsh/system
zmodload zsh/zselect
for i in /dev/input/*; do
    [[ -c "$i" ]] && sysopen -r -o cloexec -u fd "$i"
    dev=($dev[@] $fd)
done
while zselect -t $TIMEOUT $dev; do
    sleep 5
done
# timeout occurred
shutdown -h now

But this is only technically still a shell script. :)

It also won't work correctly if additional input devices are plugged in after it was started (it will ignore input on those).