Click to See Complete Forum and Search --> : idleout script


AlphaFemale
05-30-2002, 02:13 PM
Anyone know of a perl (or ?) script that I can use to run a program, and if it stays idle for too long it kills it.

Or how would I do this in perl. Im a shell script person, and dont have much exp writing perl (although this would be a good project, so if anyone wants to point me in the right direction....).

Basically I want to have a script that runs a command... say /usr/local/bin/netscape , the script should then detect when say X seconds of idletime has passed and kill /usr/local/bin/netscape

idealy, it would look something like this:

idleout -t 120 /usr/local/bin/netscape

(I would not want the script to run in the bg, I want it to run in fg. This is all part of a larger protect witch is all finished, I just want netscape to 'idleout' if no useage is detected)

thanks for any suggestions.

Stuka
05-30-2002, 03:10 PM
What do you mean by "no activity"? I think that's key to the methods available.

AlphaFemale
05-30-2002, 04:52 PM
No activity at all, as in, nobody is using it. The thing is, I hacked up a solaris x86 box, so that instead of starting up dtlogin (think xdm graphical logins), it executes a script I wrote as a specific user.

This script is just an infinte loop of it deleteing everything in the homedir for the user, and untar'ing a tarball of 'predefined' settings, then starting xinit.

Inside xinitrc it runs:
fvwm2&
netscape

(fvwm2rc is very edited to now allow any other programs to be run, etc)

This way, when you exit netscape it restarts the loop (ie deleteing all the files, and untaring 'predefined' ones).

I want a program to notice when nobody is doing anything at the console. Yes, there are very idleout programs that check to see if a user that is logged in is idle, and to boot them. This doesnt work however, since the user isnt really 'logged in' since the user is executed out of an init.d script. My easily solution, is to write a script that runs say netscape, and detects when nobody has used it for say 5 minutes. Since there is only one page they can visit, and its just to look up things quickly (ie they wont be reading or moving the mouse for other reasons). I just need to know when either the console isnt being used, or even if just netscape isnt being used.

Using it would constitute, doing things with netscape like clicking links,typing in, clicking buttons. There are no other programs that are used on this machine, so it doesnt matter that its only detecting this activity for netscape.

Once it kills netscape, it will just restart my loop, and wait all over again.

Stuka
05-30-2002, 04:59 PM
Hmmm....well, I'm not 100%, but if you captured the output from
netscape&
you'll have the PID of netscape. Then you could have your script run after netscape, and use top, or ps, or another monitoring tool, to test the usage of netscape (under the theory that an actively used program is more likely to be using CPU cycles...) and if it's below a certain threshold for so long, then exit your "idleout" script, which should take down the 'outer' script, and restart your loop.

AlphaFemale
05-30-2002, 06:06 PM
Not exactly what Im looking for, doesnt sound to clean (IE ugly hack). Im pretty sure there should be a way I can get 'idletime' from /proc/PIDNUMBER on solaris. You can always get the PID using something like NSPID=`pgrep netscape` instead of trying to capture an output from flagging.

raimi77
05-30-2002, 09:11 PM
Correct me if i'm mistaken, is this algorithm similar you are looking for?:

wait for input
read intput
while(time<some_time)
do
input
done

AlphaFemale
05-31-2002, 05:50 PM
oh duh, I just remembered.

ls -Llu /dev/kbd
ls -Llu /dev/mouse

:) that will give me the last "access time"

now with this information I can just go based on the last "access" time for these devices, and if its under Xseconds to kill a process.

Id like to learn how to do this in perl, so it would be nicer, but a quick fix I can write this in a nifty ksh script :)

[ 31 May 2002: Message edited by: AlphaFemale ]