Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

kiling idle users process but not active one

Status
Not open for further replies.

lrsteelers

Programmer
May 6, 2004
5
0
0
US
Hello,
I am trying to develop a better script to kill users who have been idle for more than one hour.
The problem is that several users are allowed to have more than one session (this is a politcal reason) So, my script kills a users idle process and their active process. here is my script,(i have made it simplistic by havine one command per line, later I will condense and combine statments)
i have just added the sorts to help with the debuggin.
finger -i > times
grep hours times > times1
sort -d -f times1 > times2
cat /times2 | tr -s ''''|cut -f1 -d' ' > /times3
sort -d -f -u times3 > times4
for i in `cat /times4`
do
ps -ef |grep $i |grep -v root > lusers2
done
sort -d -f -u lusers2 > lusers3
cat lusers3 | cut -c 9-14,35-43 > lusers4
cat times2 | cut -c 31-37 > times5
cat lusers4 | while read number
do
kill -1 $number 3> luser5
done
else
exit
fi

file contents for times1
eight pts/10 Mon Nov 08 02:51 2 hour 29 minutes
tens pts/5 Mon Nov 08 01:35 9 hours 15 minutes
quality pts/28 Mon Nov 08 4:51 6 hours 18 minutes
quality pts/29 Mon Nov 08 04:51 6 hours 18 minutes
artr pts/139 Mon Nov 08 05:38 1 hour 29 minutes

file contents for times3
eight
tens
quality
artr

file contens for luser3
artr 75200 118180 0 08:41:32 pts/139 0:00
artr 89618 91344 0 08:04:13 pts/104 0:00
eights 42720 23208 0 02:51:54 pts/10 0:02
tens 37106 37844 0 01:35:24 pts/5 0:04
tens 42317 4590 0 01:35:24 pts/20 0:04
quality 21324 23676 0 04:51:59 pts/29 0:00
quality 22732 46066 0 04:51:36 pts/28 0:00 -ksh

lusers4
75200 pts/139
89618 pts/104
42720 pts/10
37106 pts/5
42314 pts/20
21324 pts/29
22732 pts/28

file contents for times5
pts/10
pts/5
pts/139
pst/29
pts/28

Altough the user artr has two sessions, only one is idle,
I do not want to kill both of these session, just the idleone

I thought that i could do a compare of the pts commands
in the finger command to the psg commands

I would like to have the file "lusers4" to check for the pts/xx in file times5

could you please help me develop a better kill script to
kill only the idle process for each users?

Thank you for your time.

LRSTEELERS
black and gold forever
 
Use "who -u" instead of finger. It'll give you the login PID on the same line as the "idle" time.

Be sure to know your processes, though. The idle times reported by finger and who are based on line activity, not processing activity. A long running process that doesn't have any terminal output will have a high idle, even though it's burning up CPU.

One way to check for true activity is to take a couple of ps snapshots and compare the "C" fields for the process over time. If the process is active, the value of C should change almost constantly.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
I implemented this for a user who wanted all processes over 1 hour to be killed but not those processes that are under 1 hour. You will have to make any modifications to suit your own needs.

Code:
#!/bin/ksh

Ctime=$(date +%T)

__chk_time() {
CtimeH=$(echo $Ctime | awk -F: '{ print $1 }')
CtimeM=$(echo $Ctime | awk -F: '{ print $2 }')
CtimeS=$(echo $Ctime | awk -F: '{ print $3 }')
PtimeH=$(echo $Ptime | awk -F: '{ print $1 }')
PtimeM=$(echo $Ptime | awk -F: '{ print $2 }')
PtimeS=$(echo $Ptime | awk -F: '{ print $3 }')

((CtimeMinutes=CtimeH * 60))
((PtimeMinutes=PtimeH * 60))

((CtimeRunning=CtimeMinutes + CtimeM))
((PtimeRunning=PtimeMinutes + PtimeM))

if [[ $CtimeRunning -gt $PtimeRunning ]]
then
        ((TimeDiffH=(CtimeRunning - PtimeRunning) / 60))
        ((TimeDiffM=(CtimeRunning - PtimeRunning) % 60))
        TimeDiff=$TimeDiffH.$TimeDiffM
        #print TimeDiff=$TimeDiff
        return
else
        break
fi
}

__chk_proc() {
for Proc in $(ps -fu user | grep proc | awk '{ print $2 }')
do
        Ptime=$(ps -ef | grep $Proc | grep proc | awk '{ print $5 }')
        __chk_time
        if (( TimeDiff >= 1.00 ))
        then
                kill -9 $Proc
        fi
done
}

__chk_proc
 
thank you for your post,

i was able to build a better script passed on the who -u
this site has been a wonderful tool for me.


rm times4
rm times6
who -u > times
cat times | cut -c 42-46,51-55 > times2
sort -n -f times2 > times3
cat times3 | grep -E "5:|6:|4:|3:|2:" >>times4
cat times4 | cut -c 6-10 > times5
cat times5 | while read number
do
kill -1 $number 3>>times6
done
else
exit
fi

 
iknowitall,

lrsteelers only wants to kill idle processes.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
I'm okay with that [smile]. Why not try something like I have below that eliminates all of the temp files that are being created. Untested.

Code:
who -u | cut -c 42-46,51-55 | sort -n -f | grep -E "5:|6:|4:|3:|2:" | cut -c 6-10 | exec 3<&0 | read -u3 -r line | kill -6 "$line"
 
Still dangerous, though, to think of the "Idle" field as actually indicating an idle process.

That said, here's another way, which handles all idle values over one hour (1:-23:, and "old").

Code:
who -u | perl -ne '
kill "HUP", $2 if /:\d+\s+((\d+):\d+|old)\s+(\d+)/ 
                and (($1 > 0)||($1 eq "old"));
'

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

 
i agree about the not assuming the idle process is really idle .. In the past, i would have never developed this script. however, the recent upgrades to the AIX has made it possible that all of our reports/programs generate within an hour. even if the users "accidently" generates a report from the beginning of time.
the biggest problem is the lack of understanding by the new IT directory and the network team of the wan problems for our remote users/plants. Some Remote users are dropped three to four times a day. killing all these process can be time consuming.
i am still not comfortable with scheduling the idle script with crontab . But I am comfortable with manually executing this script and monitoring the results.
thank you for valuable information, i have used this website as a source of information for over year.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top