1833717 Members
2445 Online
110063 Solutions
New Discussion

Script Help

 
Peter Maitland
Occasional Advisor

Script Help

Hello Everyone,

I have an idea for a script and I am looking for help/suggestions and whether it is possible to do this. Is it possible to use the output of who -u to log people off if their idle time is greater than a certain value? I have an idea of how it could be done but my scripting abilities are not so great. Any help would be appreciated. Thank you so very much.

Peter
3 REPLIES 3
MANOJ SRIVASTAVA
Honored Contributor

Re: Script Help

Hi Peter

Try this

who -u | awk '{if ($6 > "1" ) print $7 }' > /tmp/processid
for i in 'cat /tmp/processid '
do
kill -9 $i
done

This will kill the login of all user who are logged in for more than a hour , change "1" in the script for better range.


Manoj Srivastava
Juan José Muñoz
New Member

Re: Script Help

Hello Peter

Try this script, be careful, don?t kill your job


#!/usr/bin/ksh
#########################################################
#
# Kill Sleepers Script (Run me each half hour)
#
#########################################################
TMOUT="0:30" # Replace with time to out HH:MM
SLPRS=`who -u | awk '{ if ($6 > $TMOUT) print $7 }'`
kill -9 $SLPRS

Nobody is perfect (Nobody)
Bill Hassell
Honored Contributor

Re: Script Help

Just a note for everyone:

NEVER use kill -9

When you use kill -9 you guarentee that the processes you kill will not terminate properly. Datafiles may have incomplete records, databases can have broken chains and indexes, LAN ports will be left open and so on. Always use kill -15 for idle sessions ... but be very careful you are terminating an idle shell and not a process that may require hours to rerun. In a good script, you should test that kill -15 worked (PID is gone) and if not then resort to kill -9 but be sure to email root that you had to do this.

For truly idle shells, you can set the TMOUT env value (or autologout for csh) in /etc/profile to automatically logout the users. It is ALWAYS recommended to set root's TMOUT value, about an hour or so. There is never a reason to leave any root session idle for a long time - unless security is unimportant.


Bill Hassell, sysadmin