How to monitor keyboard inactivity from script
How to monitor keyboard inactivity from script
am 02.04.2008 18:11:02 von 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
Re: How to monitor keyboard inactivity from script
am 02.04.2008 18:22:48 von jjcassidy
On Apr 2, 12:11=A0pm, mmccaws2 wrote:
> I have a user interface script that makes changes in a db or makes db
> queries. =A0I don't want the user to just leave the script running all
> the time. =A0what 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....
Re: How to monitor keyboard inactivity from script
am 02.04.2008 20:59:10 von mark
On Apr 2, 9:11=A0am, mmccaws2 wrote:
> I have a user interface script that makes changes in a db or makes db
> queries. =A0I don't want the user to just leave the script running all
> the time. =A0what 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 ;
$| =3D 1 ;
my $timeout =3D 20 * 60 ; # twenty minutes
my $rin =3D '' ;
vec($rin,fileno(STDIN),1) =3D 1;
my $ein =3D $rin ;
while () {
print "waiting for input\n" ;
my $nfound =3D select(my $rout=3D$rin, undef, my $eout=3D$ein,$timeout);=
if (vec($eout,fileno(STDIN),1)) {
die "error detected on STDIN" ;
}
elsif (vec($rout,fileno(STDIN),1)) {
my $ans =3D ;
print "got: $ans\n" ;
}
else {
print "timed out\n" ;
}
}
Re: How to monitor keyboard inactivity from script
am 02.04.2008 21:07:44 von mmccaws2
On Apr 2, 9:22 am, jjcass...@gmail.com wrote:
> On Apr 2, 12:11 pm, mmccaws2 wrote:
>
> > 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....
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
Re: How to monitor keyboard inactivity from script
am 02.04.2008 21:12:19 von mmccaws2
On Apr 2, 12:07 pm, mmccaws2 wrote:
> On Apr 2, 9:22 am, jjcass...@gmail.com wrote:
>
>
>
> > On Apr 2, 12:11 pm, mmccaws2 wrote:
>
> > > 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....
>
> 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
Re: How to monitor keyboard inactivity from script
am 02.04.2008 21:13:50 von Ben Morrow
Quoth 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?
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
Re: How to monitor keyboard inactivity from script
am 02.04.2008 21:24:13 von jurgenex
mmccaws2 wrote:
>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
Re: How to monitor keyboard inactivity from script
am 09.04.2008 02:16:51 von mmccaws2
On Apr 2, 12:24 pm, Jürgen Exner wrote:
> mmccaws2 wrote:
> >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 fo=
r
> 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 =3D time() + 300;
while (not defined ($key =3D ReadKey(-1))) {
#No key yet
my $time =3D time();
if ($time > $perltime) {exit;}
}
ReadMode 0; # reset tty
Thanks everyone.