Operating System - HP-UX
1831647 Members
2127 Online
110029 Solutions
New Discussion

controlling terminal sessions

 
SOLVED
Go to solution
Randall Lowe_1
New Member

controlling terminal sessions

I need help on writing a script that will go out and look at my terminal session activity and kill any terminal session processes that have been active for more than an hour. It needs to do this once a hour Monday through Friday between the hours of 8:00 am to 5:00 pm.
Any ideas?
11 REPLIES 11
linuxfan
Honored Contributor

Re: controlling terminal sessions

Hi,

You don't need to write a script, there is a shell variable for this called TMOUT

Set TMOUT=60 in your .profile and your session will be killed after an inactivity of 60 minutes.

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
Randall Lowe_1
New Member

Re: controlling terminal sessions

I have tried this. It only seems to work if there has NOT been any activity for an hour. I want to just end the sessions regardless.
harry d brown jr
Honored Contributor

Re: controlling terminal sessions

Isn't it rather dangerous to your applications by arbitrarily killing processes? In a university setting this "might" be acceptable, but this isn't usually a practice we do in unix.
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: controlling terminal sessions

Hi Randall:

You can see the idle time of a terminal session by looking at the sixth field from:

# who -u

This will show the time or the word "old" if the idle time is greater than 24-hours. Since the 'pid' is available as the seventh field, you could use this to kill session.

Remember that if you choose to kill the session, there could be associated processes running that will be killed. If this is still your choice, *at least* kill the session with a hangup ('kill -1 pid') and *not* a 'kill -9' which cannot be trapped to give any process a chance to cleanup files and/or shared memory. A simple kill of the session pid will not suffice.

Regards!

...JRF...
Deshpande Prashant
Honored Contributor

Re: controlling terminal sessions

Hi
Setting parameter TMOUT=300 in /etc/profile will terminate only the idle shell (ksh & sh) after 300 seconds.
Make it readonly (readonly TMOUT) in /etc/profile , so that nobody overrides it.

Thanks.
Prashant.
Take it as it comes.
harry d brown jr
Honored Contributor

Re: controlling terminal sessions

TMOUT would be nice if he was looking for a way to have idle users logged off, but TMOUT does not work if the user has spawned another task - ie: vi, emacs, sam, telnet, ...
Live Free or Die
Randall Lowe_1
New Member

Re: controlling terminal sessions

Let me give so more info about this. Maybe this will clear up some confusion.The particular terminal sessions I want to terminate are users doing quirries. They only have read only access. They are supposed to get in do their quirries and then get out. Unfortunely they don't always do this and we only have so many outside sessions to assign. All these users belong to the same group and use the same profile.
Darrell Allen
Honored Contributor

Re: controlling terminal sessions

Hi Randall,

Sounds like you want to kill all processes that have a controlling tty and have been running for at least an hour. ps -ef will show you the start time and controlling tty for processes. Your script would have to do the math to determine the elapsed time (I believe Perl would be helpful), verify you had a controlling tty, and verify you weren't killing a process you didn't really need to be killing.

That's rather scary. A rogue script of this nature can cause much damage. The chances for shooting yourself in the foot are pretty good. I believe you'd be much better off using TMOUT.

You might check with the application to see if there's a control in it for terminating inactive sessions. Perhaps the better approach is the most difficult choice: educate your users as to why they need to exit after performing their queries. Obviously that's of political nature and you probably need the right manager on the issue.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
James R. Ferguson
Acclaimed Contributor

Re: controlling terminal sessions

Hi (again) Randall:

One more suggestion: You should consider driving your script by looking for processes returned by 'ps' that have a 'uid' greater than 100 (for example); that have a controlling 'tty'; *and* have been inherited by 'init' (ppid=1).

The object is that you are then looking for "safe" (not-root;not daemon) processes that represent things like telnet sessions that have been adopted by 'init' due to abnormal termination (like a network disconnection).

Consider, too, using the UNIX95 (XPG4) option of 'ps' to return the TIME column format as "[dd-]hh:mm:ss" instead of "mmmm:ss". This may make your analysis easier.

Regards!

...JRF...
Rita C Workman
Honored Contributor
Solution

Re: controlling terminal sessions

Hi Randy,

Let me give this a thought here too...very similar to Jim's.
When you run who -u see what you can grep for to gather only these tty sessions. Example for our dial ups they always appear as wc_x_ _ _ so I would do a who -u | grep wc_x. I agree with Jim to use that idle time. So maybe a script that is something like:

who -u | grep wc_x > your who.outputfile
awk ' BEGIN {
while ( "cat who.outputfile" | getline ) {
entries++
if ( $6 > "2:00" )
print $1, $7 }
} '

Now this will display the user and the pid...and all you would have to do is ensure that is what you want BEFORE you change the script to run a kill for $7 instead of a print...I'd recommend a graceful kill not a silver (-9) bullet.

Just a thought, I'm no programmer !
Rita
Randall Lowe_1
New Member

Re: controlling terminal sessions

FINALLY!!!...A light came on out there! This is great stuff! This will get me started. It is good to know that someone out there had their right thinking cap on today!Great Work Rita!
YOU GO GIRL!!!