If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Script to log idle users off

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-16-04, 14:41
Kyzar1 Kyzar1 is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Question 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
Reply With Quote
  #2 (permalink)  
Old 07-16-04, 16:33
deebee deebee is offline
Registered User
 
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)
Reply With Quote
  #3 (permalink)  
Old 07-17-04, 14:22
fla5do fla5do is offline
Registered User
 
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
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 07-17-04 at 16:22.
Reply With Quote
  #4 (permalink)  
Old 07-19-04, 05:04
SiverSloth SiverSloth is offline
Registered User
 
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
Reply With Quote
  #5 (permalink)  
Old 07-22-04, 10:39
Kyzar1 Kyzar1 is offline
Registered User
 
Join Date: Jul 2004
Posts: 2
Quote:
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.


Quote:
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
Reply With Quote
  #6 (permalink)  
Old 07-22-04, 16:11
fla5do fla5do is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On