Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to monitor keyboard inactivity from script

3 views
Skip to first unread message

mmccaws2

unread,
Apr 2, 2008, 12:11:02 PM4/2/08
to
I have a user interface script that makes changes in a db or makes db
queries. I don't want the user to just leave the script running all
the time. what is the best way to monitor if the script has had no
actiivty for 20 minutes?

Thanks

Mike

jjca...@gmail.com

unread,
Apr 2, 2008, 12:22:48 PM4/2/08
to

You'd need to use *some kind* of persistence. Cheapest NIX-ish way is
to have a dummy file that you "touch" when you exit the program--or
whenever a significant event occurs, if you occasionally "sleep". If
you "stat" the file, you know how long it's been since it's been
"touch-ed".

Other than that, it's your choice of persistence: write and read time
to and from a file; use Storable or YAML to dump a hash with all the
values you care about; use a database....

Mark

unread,
Apr 2, 2008, 2:59:10 PM4/2/08
to

Perhaps this sample code (works on my Linux box but doesn't work on my
Windows XP box) may help:

use strict ;
use warnings ;

$| = 1 ;
my $timeout = 20 * 60 ; # twenty minutes

my $rin = '' ;
vec($rin,fileno(STDIN),1) = 1;
my $ein = $rin ;

while () {
print "waiting for input\n" ;
my $nfound = select(my $rout=$rin, undef, my $eout=$ein,$timeout);
if (vec($eout,fileno(STDIN),1)) {
die "error detected on STDIN" ;
}
elsif (vec($rout,fileno(STDIN),1)) {
my $ans = <STDIN> ;
print "got: $ans\n" ;
}
else {
print "timed out\n" ;
}
}

mmccaws2

unread,
Apr 2, 2008, 3:07:44 PM4/2/08
to

so everytime enter is hit log it and have the same script check that
or a different script monitor it? I'm not familiar with storable or
yaml, what keyword search would I use to help narrow down the choices?

Thanks

Mike

mmccaws2

unread,
Apr 2, 2008, 3:12:19 PM4/2/08
to

Mark

our posts crossed, I'll try that

Mike

Ben Morrow

unread,
Apr 2, 2008, 3:13:50 PM4/2/08
to

Quoth mmccaws2 <mmc...@comcast.net>:

perldoc -f alarm

Set an alarm for 20 minutes every time you do something. Then if the
user doesn't do anything for 20 minutes, your process gets SIGALARM: if
you just let the system handle it, your process dies; if you catch it
yourself you can do something more graceful.

Ben


>
> Thanks
>
> Mike


Jürgen Exner

unread,
Apr 2, 2008, 3:24:13 PM4/2/08
to

I can think of basically 2 different approaches:
A: the script monitors its own time ==> do not have the script wait for
user input, but check in regular intervals if input is available. If no
input, then go to sleep() again for another x seconds unless the total
wait time already exceeds those 20 minutes.
B: use a second process as the watchdog ==> whenever the script
processes some user input it will also signal the watchdog process which
will reset its internal timer. If the timer hasn't been reset for 20
minutes (perldoc -q timeout) then kill() the master script.

jue

mmccaws2

unread,
Apr 8, 2008, 8:16:51 PM4/8/08
to
On Apr 2, 12:24 pm, Jürgen Exner <jurge...@hotmail.com> wrote:

here's my half cent on this

ReadMode 4;
my $key;
my $perltime = time() + 300;
while (not defined ($key = ReadKey(-1))) {
#No key yet
my $time = time();
if ($time > $perltime) {exit;}
}
ReadMode 0; # reset tty

Thanks everyone.

0 new messages