Thanks
Mike
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....
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" ;
}
}
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
Mark
our posts crossed, I'll try that
Mike
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
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
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.