(Arrgh) Logout idle users (NOT w/ tcsh...)

(Arrgh) Logout idle users (NOT w/ tcsh...)

Post by Phil Edwar » Fri, 09 Jun 1995 04:00:00



[Crossposted to c.u.admin, c.u.osf.misc, c.u.osf.osf1, c.u.questions,
and c.u.ultrix, so don't go loopy with the followups, eh?]

The "Arrgh" is because a solution was just posted three weeks ago and
I didn't save it...

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.

Please note:  I am not asking about the autologout variable in tcsh --
I'm after something that would be run as root, separately, not
something which users would be free to configure.

We once had a program that would do this (way before my time) by way
of a system call, but when the OS was upgraded, that system call was
removed.  I'm hoping to find a replacement for the program, because
the students (and faculty especially) have an extremely annoying habit
of logging on and then sucking up CPU time like it was on a sidewalk
sale.

Please mail me with any tips, recommendations, locations of source
code, etc, you might have.  If I hear of anything definite, I will
post a summary to whichever group(s) out of the above list seem most
appropriate.

Thank you for your time and effort.  We appreciate it a great deal.

--

<URL:http://www.cs.wright.edu/people/students/pedwards/>
The gods do not protect fools.  Fools are protected by more capable fools.

 
 
 

(Arrgh) Logout idle users (NOT w/ tcsh...)

Post by Brian A. Buch » Fri, 09 Jun 1995 04:00:00


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

#

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

 
 
 

(Arrgh) Logout idle users (NOT w/ tcsh...)

Post by David Adam » Fri, 09 Jun 1995 04:00:00


  Below is some a C code I modified to run under OSF/1; it should work
for most machines. Once you get the executable do a chmod +s on it,
then make sure every user is forced to run it when they log in.

David Adamec
_______________________________

/*
**  tout - a personal timeout program
**
**  Andrew Rudoff, University of Colorado, Boulder
**
**  usage: tout minutes
**
**  This is a voluntary timeout program.  A timeout occurs when the user
**  is idle for a time larger than "minutes."  The inaccuracy can be as
**  much as 1 minute (1 minute granularity).  The user is warned 1 minute
**  before the timeout; the timeout can be prevented by typing any key to
**  clear the idle status of the terminal.
*/

#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sgtty.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#define BEEP '\007'

main(argc, argv)

int             argc;
char            **argv;

{

  char            user[50];           /* name of current user */
  char            invoker[50];        /* name of invoker */
  struct          stat statbuf;       /* buffer for stats on tty */
  long            current, timeout;   /* times in seconds */
  long            last_access,diff;   /* times in seconds */
  int             pgrp, tmp = 0;

  if (argc != 2)
    {
      fprintf(stderr, "usage: %s minutes\n", argv[0]);
      exit(1);
    }

  time(&current);
  fstat(0, &statbuf);
  diff = statbuf.st_atime-current;
  nice(4);    /* run gracefully in the background */

  if ((timeout = 60 * atoi(argv[1])) == 0)
    {
      fprintf(stderr, "%s: incomprehensible or zero timeout\n", argv[0]);
      exit(1);
    }

  if (!isatty(0))
    {
      fprintf(stderr, "%s: stdin not a terminal\n", argv[0]);
      exit(1);
    }

  strcpy(invoker, getlogin());

  printf("Idle terminal timeout set for %s (%s minutes)\n", invoker, argv[1]);
  fflush(stdout);

  if (fork())
    {
      exit(0);
    }

  while(1)
    {
      sleep(60);
      strcpy(user, getlogin());

/* Be sure same person is logged in */

      if (strcmp(user, invoker))
        {
          exit(0);
        }

      if ((fstat(0, &statbuf)) != 0)
        {
          fprintf(stderr, "%s: couldn't stat tty\n", argv[0]);
          exit(1);
        }

      time(&current);
      fstat(0, &statbuf);
      last_access = statbuf.st_atime-diff;

      if (current + 60 - last_access >= timeout)
        {
          fprintf(stderr, "\n WARNING - terminal timeout in 1 minute.%c\n",
                  BEEP);
        }

      if (current - last_access >= timeout)
        {
          fprintf(stderr, "\n\n Terminal timeout - you're outta here!\n\n");

          while(ioctl(0, TIOCGPGRP, &pgrp), pgrp != tmp)
            {
              tmp = pgrp;
              killpg(pgrp, SIGHUP);
              sleep(5);
            }

          exit(0);
        }

    }  

Quote:}

 
 
 

(Arrgh) Logout idle users (NOT w/ tcsh...)

Post by Seth J. Bradl » Fri, 09 Jun 1995 04:00:00



>  Below is some a C code I modified to run under OSF/1; it should work
>for most machines. Once you get the executable do a chmod +s on it,
>then make sure every user is forced to run it when they log in.

If one prefers a daemon-based approach, there is the public domain
Untamo program. Its very old code, and I haven't seen it used in
awhile, but it still should work for many systems.
--
Seth J. Bradley, Senior System Administrator, Intel CT

----------------------------------------
   "How can you be in two places at once when you're not anywhere at all"
        - Firesign Theater
 
 
 

(Arrgh) Logout idle users (NOT w/ tcsh...)

Post by Phil Farrel » Sat, 10 Jun 1995 04:00:00


I wrote a "new" awk script to find and kill idle
login sessions.  I run it once per hour from cron.  It has provisions
for exempting daemons and special users, as well as special programs
that may have to run in the foreground but leave the terminal idle
for long periods.  It uses "w", "ps", and awk to do its work.
I wrote it for Ultrix and it will probably need some minor changes
for other systems.  You can get a copy via anonymous ftp to
pangea.stanford.edu, in pub/idlekiller.tar.

Phil Farrell, Computer Systems Manager
Stanford University School of Earth Sciences

 
 
 

1. Untamo system call? /was: (Arrgh) Logout idle users (NOT w/ tcsh)

        (I tried doing a followup to this message but the followup went
        to Phil Edwards via mail instead of the newsgroups).

        The old versions of untamo will not work under OSF/1 because
        the process table stuctures and the method to access information
        in them have changed (i.e. OSF/1 uses the mach kernel instead of
        the older unix kernels).

        Anyway, I did a quick hack on a version of untamo that I got off
        of UCLA's anon ftp server about 1.5 years ago to make it run under
        OSF/1 (I used the process table routines from the SPS utility also).
        It works faily well for us (it does need to be a bit more forceful
        with some processes that ignore SIGHUP, though) and has been in use
        here since we got our alpha systems. I have put it out on our ftp
        server:

                ftp.nevada.edu:/pub/unix-stuff/alpha-untamo.tar.gz

        The software is available AS IS. It works for us (it is currently
        running under OSF/1 3.0 and has run under 1.3). Since it has not
        been run anywhere else, it may or may not work for you. I will not
        be able to provide much assistance for you to get it running. However
        I would like any fixes and enhancements that you make in order to
        fold them back into the distribution...

        -ron young

2. how to: virtual device driver

3. Untamo system call? /was: (Arrgh) Logout idle users (NOT w/ tcsh...)

4. su/rsh as root won't work for users with passwords?

5. Howto auto logout idle users ?

6. Why route would drop info?

7. Auto logout of idle users?

8. WU FTP server

9. How to logout an idle user?

10. How to logout idle users

11. Logout idle users - HOW-TO?

12. How to logout an idle user?

13. Program to logout idle users