Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: babucher@mtu.edu (Brian A. Bucher)
Subject: Re: (Arrgh) Logout idle users (NOT w/ tcsh...)
Date: 1995/06/08	[thread overview]
Message-ID: <3r78pe$bv2@news.mtu.edu> (raw)
In-Reply-To: 3r66od$dj1@valhalla.cs.wright.edu

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$$



  reply	other threads:[~1995-06-08  4:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-06-08  4:00 (Arrgh) Logout idle users (NOT w/ tcsh...) Phil Edwards
1995-06-08  4:00 ` Brian A. Bucher [this message]
1995-06-08  4:00   ` David Adamec
1995-06-08  4:00     ` Seth J. Bradley
1995-06-09  4:00       ` Phil Farrell
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox