From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,e34f8bf95ca3b17 X-Google-Attributes: gidfee84,public X-Google-Thread: f678f,e34f8bf95ca3b17 X-Google-Attributes: gidf678f,public X-Google-Thread: 1020c6,e34f8bf95ca3b17 X-Google-Attributes: gid1020c6,public X-Google-Thread: f8c9b,e34f8bf95ca3b17 X-Google-Attributes: gidf8c9b,public X-Google-Thread: 10b612,e34f8bf95ca3b17 X-Google-Attributes: gid10b612,public From: babucher@mtu.edu (Brian A. Bucher) Subject: Re: (Arrgh) Logout idle users (NOT w/ tcsh...) Date: 1995/06/08 Message-ID: <3r78pe$bv2@news.mtu.edu>#1/1 X-Deja-AN: 104035933 references: <3r66od$dj1@valhalla.cs.wright.edu> followup-to: comp.unix.admin,comp.unix.osf.misc,comp.unix.osf.osf1,comp.unix.questions,comp.unix.ultrix organization: Michigan Technological University newsgroups: comp.unix.admin,comp.unix.osf.misc,comp.unix.osf.osf1,comp.unix.questions,comp.unix.ultrix Phil Edwards (pedwards@valhalla.cs.wright.edu) wrote: : : Anyhow, I'm looking for a program that will automatically log out : users if they remain idle for a specified period of time. We have : multiple Unix boxes here, so I'm looking for something that would work : on either OSF/1, Ultrix, perhaps SunOS; or even a combination of the : above if it could be ported. Below is a script (my first true script ever really) that should do what you need it to. You will want to put in a check to make sure you don't kill root or any of the sysops (tis a Bad Thing). It was written for /bin/sh so you shouldn't have any problems. I'm posting this partly because I would like a reaction from the scripting Gurus out there on how I did on my first attempt. How would you improve this, and do I have hope for the future? ;) Thanks, Boo ---cut here--- #!/bin/sh # # Script written by Brian Bucher (babucher@mtu.edu) used to log out # idle users. (30 minutes) # # trapping escape signals so that we can clean up the /tmp dir trap "/bin/rm -fr /tmp/wfile$$ /tmp/upid$$; exit" 0 1 2 15 # creating a unique file in /tmp - delete the first 2 lines (garbage) w | cut -c1-8,42-43 | sed '1,2d' > /tmp/wfile$$ # reading and assigning a username and idletime cat /tmp/wfile$$ | while read line do USER=`echo $line | cut -d' ' -f1` IDLETIME=`echo $line | cut -d' ' -f2` # If the idletime is less than 30 minutes, delete all occurences of the username # from the temporary file if [ "$IDLETIME" -lt 30 ] then sed "/$USER/d" /tmp/wfile$$ > /tmp/wfile2$$ mv /tmp/wfile2$$ /tmp/wfile$$ fi done # If there are any users left in the temporary file, they must # have had processes idle over 30 minutes, so kill them. # First check to see if the file is empty, if it is, then exit with # status 1 if [ ! -s /tmp/wfile$$ ] then exit 1 fi # If not empty... # ...First, get the user... cat /tmp/wfile$$ | while read line do USER2=`echo $line | cut -d' ' -f1` # ...then get his processes and store in temporary file ps -guaxw | grep $USER2 | cut -c10-14 > /tmp/upid$$ cat /tmp/upid$$ | # now kill each process (note: this my attempt to kill processes that # were already killed) # if processes are not present, redirect stderr to /dev/null while read USERPID do echo "Killing PID $USERPID" # kill $USERPID >& /dev/null done # Cleanup time! /bin/rm -fr /tmp/wfile$$ /tmp/upid$$