To combat this, I wrote the following code. It checks the last time the tty was written to and if that time is greater than the specified time it either writes a message to the screen (verbose mode) or it just sends a null ("\0"). Either way, the access time of the tty is updated.
The useage is quite simple, all you have to do (minimally) is place this line in your .profile (.bash_profile etc)
'/path/to/file/ka.pl &'.
This will run with the default of no visable output checking every 20 seconds for 60 seconds of idle time on the tty.
To enter you own times the syntax is
'/path/to/file/ka.pl <loop seconds> <seconds of idle time>'
This 'ka.pl 20 60' is the exact same as the default. To check every 60 seconds for a tty that has been idle for 3 minutes the syntax is 'ka.pl 60 180'.
If you wish to see when the program kicks in and get a running count of how many seconds it has been holding you can turn on verbose mode.
Verbose mode is not a specific operator, instead the program simply checks to see if a third argurment has been put on the command line. The argument can be anything ( 1, 0, cheezewhiz, etc). All the program checks for is that $verbose is defined and if it is, it prints a text line to the screen instead of a null.
This has only been tested on Solaris 8 (Sparc) and Red Hat 7.x. There have been no problems and it should run on most *nix platforms with no problems. Found a circumstance under bash on Red Hat and FreeBSD. The bash shell does not automatically send a 'HUP' to the background processes on exit. To make it do this you need to turn on the shell feature. This is done with 'shopt -s huponexit' in your .profile or /etc/profile if you want it for all users.
I have not yet refined the reporting of cumulative time in verbose mode. The problem in accurate reporting occurs when 'loop seconds' is greater than 'idle seconds'. Normally you would not want to do this anyway, but if you do the program will let you, but it issues a warning that the reporting may not be correct.
#!/usr/bin/perl -w #----------------------------------------------------------------# # The purpose of this script is to monitor the activity on a # # terminal. if the terminal is idle, it will send a text string # # or a null to the tty to keep the session open. Once the # # terminal is no longer idle it will sleep and test again in # # $loop seconds. # #----------------------------------------------------------------# use strict; use POSIX; my $loop = shift || 20; # number of seconds between checks my $sec_old = shift || 60; # number of seconds since data has # come through the port tty my $verbose = shift; # check for verbose output my $tty = ttyname(1); # '1' is the file descriptor for STDOUT my $total_sleep_seconds = 0; $|++; warn "\nHold time reporting is not accurate when loop", " seconds ( $loop ) ", "are greater than seconds old ", "( $sec_old )\n" if ( $loop > $sec_old ); while (1){ my $age = time() - eval((stat($tty))[9]); if ( $age > $sec_old){ $total_sleep_seconds += $age; print "Holding Session $total_sleep_seconds seconds.\n" if (defined $verbose); print "\0" if (! (defined $verbose)); } elsif ($age < $loop){ $total_sleep_seconds = 0; } sleep $loop; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Keeping interactive net session alive
by merlyn (Sage) on Oct 30, 2002 at 16:54 UTC | |
by gnu@perl (Pilgrim) on Oct 30, 2002 at 18:48 UTC |