Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!sun-barr!apple!lamc!kdavis From: kdavis@lamc.UUCP (Ken Davis) Newsgroups: comp.sys.att,u3b.misc Subject: Info-3b2 Digest, Number 85 Message-ID: <202012@lamc.UUCP> Date: 22 May 89 15:54:18 GMT Reply-To: kdavis@lamc.UUCP (Ken Davis) Organization: Letterman Army Medical Center - San Francisco, CA Lines: 197 Info-3b2 Digest, Number 85 Monday, May 22nd 1989 Today's Topics: Re: idledaemon Re: idledaemon Wanted: 3b2/500, 600 or larger Wanted: 3b2/500, 600 or larger ---------------------------------------------------------------------- Subject: Re: idledaemon From: info-3b2@netsys.COM Date: Fri, 19 May 89 10:16:18 -0400 Subject: Re: idledaemon Date: Fri, 19 May 89 10:08:33 EDT From: "Tim J Ihde" Here's a relativly flexible idle-killer that I wrote a while back. I run it from cron every now and then to kill off various users. We can only have so many people logged in via our STARLAN, so I usually kill them quickly (15 min). Incoming modems are always a premium, so they go as well. Regular tty's I don't worry about too much, so they can be idle for 45 minutes. The pattern for who to kill is passed to lcheck.sh as the first argument. This pattern will be used by egrep on the output of a "who -uTH" to find who to kill. With this setup you can put comments in /etc/inittab to match on - for example I put "modem xxx-xxxx" in the file, and then I can use 'modem' as a pattern to lcheck.sh for killing users logged in via modems. The second argument passed is the maximum idle time to allow. There are some tunables here - you can specify certain users or certain processes that will never be killed. This tries to be friendly, and kill first with a SIGHUP. Most programs (notibly vi) will clean up before terminating with this signal. Vi will at least save the file the person was editing instead of losing it. If the SIGHUP doesn't work then SIGKILL is used. A logfile "lcheck.log" is maintained containing information about who and what was killed. Data can also be mailed to root if desired. Here are the cron entries I use at the moment: 1,11,21,31,41,51 * * * * /usr/root/PM-D/lcheck.sh 'slan|modem' >/dev/null 5,35 * * * * /usr/root/PM-D/lcheck.sh 'tty' 45 >/dev/null And here's the program: #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # lcheck.sh # if test -f 'lcheck.sh' then echo shar: will not over-write existing file "'lcheck.sh'" else echo x - 'lcheck.sh' sed 's/^X//' >'lcheck.sh' << 'SHAR_EOF' X### X# X# name lcheck.sh - check for idle users X# X# synopsis lcheck.sh X# X# author Tim J Ihde X# X### X XWORKDIR=/usr/root/PM-D XPATTERN=${1:-slan} #pattern to match, def 'slan' XUIGNORE="tim" #never kill these users XPIGNORE="ttysrv|uucico|UUCICO|rnews" #never kill these processes XMAXIDLE=${2:-15} #max min. of idle time, def 15 minutes X Xecho "PATTERN - [$PATTERN]" Xecho "MAXIDLE - [$MAXIDLE]" X Xcd $WORKDIR Xrm -f lcheck.now killps X X# X# pass one: politely suggest that these processes go away X# Xwho -uTH | Xegrep "$PATTERN" | Xegrep -v "$UIGNORE" | Xcut -c1-9,12-18,37-50 | Xsed ' /\./D X s/:// X ' | Xwhile read login ttyname itime pid Xdo X if [ $itime = "old" -o $itime -gt $MAXIDLE ] X then X who -uTH | grep $ttyname | tee -a lcheck.log X ps -ft$ttyname X ps -t$ttyname | tail +2 | egrep -v "$PIGNORE" | X while read procid stuff X do X echo $procid >>killps X kill -1 $procid 2>/dev/null X [ $? -eq 0 ] && echo "1 -> $procid" X done X ps -ft$ttyname X fi Xdone > lcheck.now X X# wait for them to die Xsleep 5 X X# X# pass two: no more mister nice guy X# X Xif [ -s killps ] Xthen X while read procid X do X kill -9 $procid 2>/dev/null X [ $? -eq 0 ] && echo "9 -> $procid" X done < killps Xfi >> lcheck.now X X#print report if we did anything X[ -s lcheck.now ] && cat lcheck.now X X#or mail the report here if you prefer X#if [ -s lcheck.now ] X#then X #mailx -s'login_killed' root