Kill idle script is killing unnecessarly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill idle script is killing unnecessarly
# 1  
Old 03-04-2010
Question Kill idle script is killing unnecessarly

Hi All,I have a problem with my kill idle script.my script is supposed to kill the user sessions which are idle for more than 2 hours.But is is killing the sessions which are idle for less than 2 hrs also.I dont know the exact time after which the script is killing,but it is less than 2 hours i am sure.I faced this problem personelly with my session also.i opened a new session of my application and after half an hour or so it got killed by the shell idle script.Here I am giving my kill idle script.Please if any one can make it out where the problem is that could be great.

Code:
#!/bin/sh
# This script will kill the users listed in the file $SELECTUSERS that are idle for the time equal to or greater then $IDLETIME. IDLETIME is in hours
. /run/pronto/lib/sh_environs 
IDLETIME=2; export IDLETIME
# ERRORLOG=$PRONTO/lib/kill-idle/error.log; export ERRORLOG
KILLIDLELOG=$PRONTO/lib/kill-idle/kill-idle.log; export KILLIDLELOG
USERSLOG=$PRONTO/lib/kill-idle/users.log; export USERSLOG
SELECTUSERS=$PRONTO/lib/kill-idle/users; export SELECTUSERS 
# below is for sunos
who -u | /usr/xpg4/bin/grep -i -f $SELECTUSERS|/usr/xpg4/bin/awk '{print $1 "," $2 "," $6 "," $7}' > $USERSLOG
for line in `cat "$USERSLOG"`
do
NAME=`echo $line | /usr/xpg4/bin/awk -F "," '{print $1}'`
PTS=`echo $line | /usr/xpg4/bin/awk -F "," '{print $2}'`
##Below is for hrs timeout
TIME=`echo $line | /usr/xpg4/bin/awk -F "," '{print $3}' | /usr/xpg4/bin/awk -F ":" '{print $1}'`
## Below is for minutes timeout
# TIME=`echo $line | /usr/xpg4/bin/awk -F "," '{print $3}' | /usr/xpg4/bin/awk -F ":" '{print $2}'`
if [ "$TIME" -ge "$IDLETIME" ] 
then
echo `date` >> $KILLIDLELOG
echo "PTS = $PTS" >>$KILLIDLELOG
PID=`ps -ef | /usr/xpg4/bin/grep "$PTS" | /usr/xpg4/bin/grep "pronto" | /usr/xpg4/bin/awk '{print $2}' | head -1`
echo "PID is $PID" >>$KILLIDLELOG
echo "name is $NAME" >>$KILLIDLELOG
echo "Idle time is $TIME hrs" >>$KILLIDLELOG
echo "kill $PID" >>$KILLIDLELOG
echo "" >>$KILLIDLELOG
kill -5 $PID
fi
done
exit


Last edited by pludi; 03-04-2010 at 08:00 AM.. Reason: code tags, please...
# 2  
Old 03-04-2010
First, $3 is not giving the time but date.
second, you need to calculate the time by substrating "time now" with the time the user logged in and compare that against the specified time.
# 3  
Old 03-04-2010
hi devtakh,

I could not get you ....so can you please expalin me where i am missing the logic and explain the way to rectify the issue.

many thanks,
prabhu
# 4  
Old 03-04-2010
Some areas where the script could go wrong:

1) If is run from cron too frequently runs could overlap.

2)
Quote:
who -u | /usr/xpg4/bin/grep -i -f $SELECTUSERS|/usr/xpg4/bin/awk '{print $1 "," $2 "," $6 "," $7}' > $USERSLOG
If one username is contained in another user name you could get a false match. e.g. "fred" and "alfred" . Depends on what your usernames are like.


3)
Quote:
PID=`ps -ef | /usr/xpg4/bin/grep "$PTS" | /usr/xpg4/bin/grep "pronto" | /usr/xpg4/bin/awk '{print $2}' | head -1`
Lots of scope for false mismatches. Consider similar PTS values "pts/aa" and "pts/aab" .
No point in running "ps -ef" because we already know $PTS. Try "ps -ft"${PTS}". This will make the script faster and reduce the chance of false matches.
Also the the second "grep" in the pipeline could find the first "grep" in the pipeline.

Could be better with something like this which allows for the user to log out since we started our script. Also reduces chance of false matches. I don't know whether the "head -1" is needed as I know nothing about your process table.

Quote:
ps -ft"${PTS}" 2>/dev/null | /usr/xpg4/bin/grep "pronto" | /usr/xpg4/bin/grep -v "grep" | /usr/xpg4/bin/awk '{print $2}' | head -1 | while read PID
do
echo "PID is $PID" >>$KILLIDLELOG
echo "name is $NAME" >>$KILLIDLELOG
echo "Idle time is $TIME hrs" >>$KILLIDLELOG
echo "kill $PID" >>$KILLIDLELOG
echo "" >>$KILLIDLELOG
kill -5 $PID
done
I have never issued a "kill -5" but I assume that it means something to "pronto"?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Trapping a kill command sent to a script and using it to send an EOF to a subprocess before killing

I originally had a script written in pure shell that I used to parse logs in real time and create a pipe delimited file that only contained errors. It worked but it was using a lot of memory (still not clear on why). I originally got around this by writing a wrapper for the script that ran on cron... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

2. Shell Programming and Scripting

Kill idle Process using a script

Hi, I need a script that can automatically kill all processes named "webrepn" and "webrebw" if idle for more than 30 minutes. Then I will have a Cron Job to run the script every night or 2-3 times a day depends on how this script helps. Right now, I run "ps -ef | grep webrebn" and "kill -9... (7 Replies)
Discussion started by: MaggieL
7 Replies

3. Solaris

Kill idle dt session

Hi, i need to kill the idle dt sessions through script, can anyone tell me how to do? RJS (2 Replies)
Discussion started by: rajasekg
2 Replies

4. AIX

Kill IDLE Process using script !!!

Dear Friends , I am using DB2 database in AIX 5.3 server . In my server some IDLE process are generated after several times which I need to kill it manually each and every time . The process I query like following : root@bagpuss $ ps auxw|sort -r +3|head -10 USER PID %CPU %MEM ... (3 Replies)
Discussion started by: shipon_97
3 Replies

5. HP-UX

kill idle users

Hi, In my network we uses the NetTerm program to connect us to HP-UX 10.x server from windows workstations, but in some cases the user doesn't logout and close it by window's x button. The problem is that in HP-UX the user and all his tasks remain active and when he enter again HP-UX creates a... (12 Replies)
Discussion started by: efrenba
12 Replies

6. Shell Programming and Scripting

Killing idle users TIA

I wrote a script to kill users idle more than 1/2 hour, ignoring those in an exception list. Here is the script as it is now: #! /usr/bin/awk -f BEGIN { system("who -u | sort +5 > /tmp/loginfile"); system("echo User Sessions Killed > /tmp/killedlogins"); ... (2 Replies)
Discussion started by: PapaBear
2 Replies

7. Shell Programming and Scripting

script for killing idle users

I need a script that will look for idle users and kill there proc. (7 Replies)
Discussion started by: jdel80
7 Replies

8. HP-UX

Is there a script available to kill Idle users

My max user parm is set to 1050. I'm currently at 1038 this is causing major slow downs on the server. I looking for a way log off "idle" user logins with out having to do it individually. :confused: (5 Replies)
Discussion started by: rfmurphy_6
5 Replies

9. UNIX for Dummies Questions & Answers

Killing idle user processes

I'm looking for some help, please! I'm trying to kill any idle user processes over 40 Minutes. I have tried putting TMOUT=2400 within the users .profile However this does not seem to be working. We run aix 4.3.3 with ORACLE 7.3 The above works o.k. when the user is only within the... (3 Replies)
Discussion started by: annette
3 Replies

10. Cybersecurity

Killing Idle session

does any one know how to kill an idle session? I want to kill any idle sessions after 30 min... Local or remote.... i want to do this without a script or TCP wrappers...i want to know if there is a file that i can configure..... ThAnks:rolleyes: (4 Replies)
Discussion started by: securhack
4 Replies
Login or Register to Ask a Question