How to monitor keyboard inactivity from script

M

mmccaws2

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
 
J

jjcassidy

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

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

Mark

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

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" ;
}
}
 
M

mmccaws2

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

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
 
M

mmccaws2

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
 
B

Ben Morrow

Quoth mmccaws2 said:
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?

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
 
J

Jürgen Exner

mmccaws2 said:
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?

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
 
M

mmccaws2

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top