Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2004
    Posts
    2

    Question Unanswered: Script to log idle users off

    I'm new to Unix shell scripting so bear with me please. I'm trying to create a script that will check if users have been idle for an hour or more and log them off (kill). Below is the code I have so far followed by the syntax error I receive. Any help would be appreciated.

    Code:
    #!/bin/sh
    who -u | cut -c 1-10,39,50 | grep 1: > current
    for IDLE_USER ('cat current | awk '{print $3}'`)
    do
         kill -9 $IDLE_USER
    done
    end 
    exit (0)
    And here is the syntax error
    Code:
    Syntax Error at line 3: `(' unexpected
    Thanks,
    Kyzar1

  2. #2
    Join Date
    Jul 2004
    Posts
    45
    use ` (the key pad contains ~) instead of ' (single qoute) at line 3 (i have not checked this script but i am guessing )

    #!/bin/sh
    who -u | cut -c 1-10,39,50 | grep 1: > current
    for IDLE_USER (`cat current | awk '{print $3}'`)
    do
    kill -9 $IDLE_USER
    done
    end
    exit (0)

  3. #3
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi Kyzar1,
    what does it mean in your example ? "grep :1"
    what do you want to do with this command

    try this :

    #my "who -u" looks like this
    #medcom2 ttyp0 Jul 17 20:15 . 1425
    #$1 user
    #$2 tty
    #$3 month
    #$4 day
    #$5 login time
    #$6 current
    #$7 PID


    #!/bin/sh
    PID=`who -u | grep "1:" awk '{ print $7 }'`
    echo $PID
    for IDLE_USER in $PID
    do
    kill -9 $IDLE_USER
    done
    exit 0
    Last edited by fla5do; 07-17-04 at 17:22.
    Greetings from germany
    Peter F.

  4. #4
    Join Date
    Jun 2004
    Posts
    29

    Use the built in facilities

    Far more elegant is to use the facilities built into the OS. I use AIX where you set TMOUT=<time in seconds>
    i.e. if you put
    export TMOUT=600
    in /etc/profile then users will be logged off after 10 mins idle time. Other flavours of Unix use different methods

  5. #5
    Join Date
    Jul 2004
    Posts
    2
    what does it mean in your example ? "grep :1"
    I'm trying to grab all users that have an idle time of 1:00 or greater. I figure if the script is run once an hour I shouldn't have to worry about the possibilty of an idle time that is 2:xx, 3:xx, etc.

    I tried the code you listed but as soon as I ran it, it killed my session. No other sessions were killed though, just mine. I even altered the code to echo each PID instead of kill -9 them and it did the same thing.


    Far more elegant is to use the facilities built into the OS. I use AIX where you set TMOUT=<time in seconds>
    i.e. if you put
    export TMOUT=600
    in /etc/profile then users will be logged off after 10 mins idle time. Other flavours of Unix use different methods

    I'm using SCO Unix and I think our vendor might use a custom shell, which does call /etc/profile. I added the line you mentioned, but it didn't work, just prevented users from logging in. I'll try to track down the correct syntax to make sure it's right for my system.

    Thanks for all the help thus far!

    Kyzar1

  6. #6
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138
    Hi Kyzar1,

    I use SCO Unix too ( SCO Open Server Enterprise V5 )

    Try this :

    #my "who -u" looks like this
    #medcom2 ttyp0 Jul 17 20:15 . 1425
    #$1 user
    #$2 tty
    #$3 month
    #$4 day
    #$5 login time
    #$6 current
    #$7 PID


    #!/bin/sh

    # use this when the command comes from the crontab
    # MY_TTY="ttyp0" # example for your tty

    # use this when the command comes from your shell
    MY_TTY=`tty | sed 's:/dev/::'`

    PID=`who -u | grep "1:" | grep -v $MY_TTY | awk '{ print $7 }'`
    echo $PID
    for IDLE_USER in $PID
    do
    kill -9 $IDLE_USER
    done
    exit 0

    # remember : the grep command "1:" is also given at the hour "21:00-21:59"

    # may be : in my first solution your shell is the first which be killed
    # and then the following commands does not will be execute, because your
    # shell is dead in this moment.

    # Try this !!!!!!!!!!!
    Greetings from germany
    Peter F.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •