#!/bin/tcsh # Idle Killer -- To be run from crontab (root) every 10 min or so. # Written by fairlite@mtech.csd.uwm.edu on 12/17/94 # Kills only processes attached to tty of sessions idle for more than # allowed maximum by the idle user. # The next line contains the value of Maximum Idle Minutes set maxidle=60 # The next line contains the location of the exemption file set exemptfile=/etc/idlekiller.exempt # Set up a space for the logfile... set tmplog=/tmp/$$.ikiller # Initialize mail flag... set domail="n" # Look at a w listing... /usr/bin/w |tail +3 >>/tmp/ikiller.$$.w set count=1 set total=`wc -l /tmp/ikiller.$$.w |awk '{print $1}'` loop: # read a line set session=`/bin/head -${count} /tmp/ikiller.$$.w |/usr/bin/tail -1` # get information from w listing... set tty=`echo ${session} |/usr/bin/awk '{print $2}'` set pstty=`echo ${tty} |sed "s:tty::g"` set idle=`echo ${session} |/usr/bin/awk '{print $5}'` set login=`echo ${session} |/usr/bin/awk '{print $1}'` # determine minutes idle... set minutes=`echo ${idle} |/usr/bin/sed "s/.*://g"` set hours=`echo ${idle} |/usr/bin/sed "s/:.*//g"` set nohour=`echo ${idle} |/usr/bin/grep ":"` if ("${nohour}" != "") then set hourmins="`/usr/bin/expr ${hours} \* 60`" @ minutes+=${hourmins} endif # see if exempt... set exempt=`/usr/bin/grep ^${login}$ ${exemptfile}` # if not, and over maxidle then... if (("${exempt}" == "") && (${minutes} >= ${maxidle})) then # set flag that we need to send mail set domail="y" # log goodies for root to see later... echo "Killed user: ${login}" >>${tmplog} set date=`/bin/date` echo " on tty: ${tty}" >>${tmplog} echo " at: ${date}" >>${tmplog} echo " idle for: ${minutes} minutes." >>${tmplog} echo "" >>${tmplog} echo "Processes killed:" >>${tmplog} echo "" >>${tmplog} /bin/ps -auxww |/usr/bin/grep "^${login} " |/usr/bin/grep "${pstty}" >>${tmplog} echo "" >>${tmplog} echo "------------------------------------------------------------------------------" >>${tmplog} echo "" >>${tmplog} foreach process (`/bin/ps -aux |/usr/bin/grep "^${login} " |/usr/bin/grep "${pstty}" |/usr/bin/awk '{print $2}'`) # Kill! Kill! Kill! (See "Thieves" by Ministry!) /bin/kill -9 ${process} end endif if (${count} < ${total}) then @ count+=1 goto loop endif # If necessary, handle the goodies... if ("${domail}" == "y") then echo "[Processed by: Idle Killer v1.0 -- Fairlight]" >>${tmplog} # Mail the goodies back to root... /bin/cat ${tmplog} |/usr/bin/elm -s Killed-Idler root >&/dev/null # Clean up our mess... /bin/rm -f ${tmplog} endif # Go away... exit 0