1825693 Members
3248 Online
109686 Solutions
New Discussion

Re: kill idle user

 
SOLVED
Go to solution
peterchu
Super Advisor

kill idle user

I hv a script to kill the idle user,

for x in ` who -u |awk ........ `
do

idleuser = ......

if [[ ...... ]]
then
kill $idleuser
fi
done

If I want to add a user list so that the user in the list will be be killed by the script , could suggest how to modify the script ? thx.
35 REPLIES 35
Sridhar Bhaskarla
Honored Contributor
Solution

Re: kill idle user

Hi,

How are you getting the idle user?

To basically read from a file you can use either 'for' or 'while'. There are otherways too. With 'for'

for user in $(cat user_list)
do
idleuser=...
..
done

With 'while'

while read user
do
idleuser=...
...
done < user_list

The above was assuming that the users are speicified with one user per line.

If you post your complete script, then it will be much easier for us to suggest working solutions.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
ramkumar
Valued Contributor

Re: kill idle user

Hi
I am able to under stand whats is your exact requirement any how go through the below script

collect all the users inthe system in to file say usrlst

cut -f1 -d : /etc/passwd > usrlst

for i in `cat usrlst`
do
ps $i |cut -f1 -d' ' | fuser -k
done
this will kill all the users those logged in the system . .if you want to modify usrlist you can change the users in the list
peterchu
Super Advisor

Re: kill idle user

thx replies,

May be I state my requirement more clearly , I want to kill the idle user but ignore to kill some user who are in A/C , EDP and Engineer department , what can I do ? thx.
Sridhar Bhaskarla
Honored Contributor

Re: kill idle user

Hi,

Then you have to somehow identify them by something. Say you add them all to a list say 'excludeusers.list' in the following format.

user1:Finance
user2:Engineering
user3:Finance
user4:Finance

etc.,

Then do the following

for x in `who -u|awk ...` #<-- I assume you are getting the user here"
do
grep -q "^${x}:" excludeusers.list
if [ $? != 0 ]
then

idleuser="
..
..
if [[ ...... ]]
then
kill $idleuser
fi



fi
done


Again, unless you post your exact script, these solutions may not work because we don't know how your script is gathering the data.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
peterchu
Super Advisor

Re: kill idle user

thx reply , but I am not familiar with script writing

the bwlow is my script , could suggest how to modify it ? thx

for x in `who -u | awk '{ print $6":"$7 }' | grep -v "\."| awk -F : '{ print
($1*60)+$2":"$3 }'`

do
time=`echo $x | awk -F ":" '{ print $1 }'`
pid=`echo $x | awk -F ":" '{ print $2 }'`


if [[ $time -gt 120 ]]
then
kill -1 $pid
fi
done
Muthukumar_5
Honored Contributor

Re: kill idle user

We can do this as,

#!/bin/sh
who -u | while read line
do
time=`echo $line | grep -v "/." | awk '{ split($6,a,":"); print (a[1]*60)+a[2] }'`
pid=`echo $line | awk '{ print $7 }'`
if [[ $time -gt 120 ]]
then
kill -1 $pid
fi
done

It will kill all session's ID with is idle more than 120 seconds there.


Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: kill idle user

Sorry.

Remove grep -v "/." there and try as

#!/bin/sh
set -x
who -u | while read line
do
time=`echo $line | awk '{ split($6,a,":"); print (a[1]*60)+a[2] }'`
pid=`echo $line | awk '{ print $7 }'`
if [[ $time -gt 120 ]]
then
kill -1 $pid
fi
done

It will work there.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: kill idle user

Peter,

We can not use for loop to get a line of words separated with ' ' ( space there ) then it will print word by word there.

Example:

for x `who -u`
do
echo $x
done

It will display word by word.

To do your requirement in one liner then,

kill -1 `who -u | awk '{ if ( $6 != "." ) { split($6,a,":");if ( a[1]*60+a[2]>=120) print $7 } }'`

HTH.
Easy to suggest when don't know about the problem!
peterchu
Super Advisor

Re: kill idle user

thx Muthukumar reply ,

If I want to exclude the users in some department , what can I do ? thx.
Sridhar Bhaskarla
Honored Contributor

Re: kill idle user

Hi,

There are couple of issues with the script.

1. The activity period in 'who -u' is displayed as 'old' if the inactivity is more than for 24 hrs.

2. Users that were not idle within the last minute will be displayed with a "." in their activity field.

Keeping in view the above, below is an easily understandable script. Create a file say /etc/exception.lst with the following format

user1:Engineering
user2:Finance
user3:Finanance

Try it as it is. Once you are satisfied, then remove the comment for the lines that has 'kill_pid' in the last 'if' statement.

-Sri

#!/usr/bin/sh

PATH=$PATH:/usr/bin:/usr/sbin:/usr/contrib/bin

TIMEOUT=120
EXCEPTIONLIST=/etc/exception.lst
touch $EXCEPTIONLIST

kill_pid()
{
PID=$1
kill -9 $PID
}

echo "User:Activity:PID:Status"
who -u | while read line
do
time=$(echo $line | awk '{print $6}')
pid=$(echo $line | awk '{print $7}')
user=$(echo $line | awk '{print $1}')

grep -q "^${user}:" $EXCEPTIONLIST
if [ $? != 0 ]
then
if [ "$time" = "old" ]
then
(( time = $TIMEOUT + 1 ))
else
echo $time |grep -q "\."
if [ $? != 0 ]
then
time=$(echo $time|awk '{FS=":";print $1*60+$2}')
else
time=0
fi
fi
fi


if [ $time -gt $TIMEOUT ]
then
echo "$user:$time:$pid:Killed"
#kill_pid $pid
else
echo "$user:$time:$pid:Untouched"
fi
You may be disappointed if you fail, but you are doomed if you don't try
Muthukumar_5
Honored Contributor

Re: kill idle user

We can do this as,

#!/bin/sh
set -x

# Fill exception user list
USERLIST="test user peter"

set -A ARR $USERLIST

who -u | while read line
do

user=`echo $line | awk '{ print $1 }'
time=`echo $line | awk '{ split($6,a,":"); print (a[1]*60)+a[2] }'`
pid=`echo $line | awk '{ print $7 }'`

index=0
while [[ $index -lt ${#ARR[*]} ]]
do

if [[ $user != "${ARR[$index]}" ]]
then
if [[ $time -gt 120 ]]
then
kill -1 $pid
fi
fi

let index=index+1

done

Fill the needed user on USERLIST variable there.

HTH.
Easy to suggest when don't know about the problem!
peterchu
Super Advisor

Re: kill idle user

hi Muth,

I testing r method , but pop the below message


+ USERLIST=test user peter
+ set -A ARR test user peter
./ab: line 7: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
James OReilly
Occasional Contributor

Re: kill idle user


Place in users .profile a timeout or
even in /etc/profile for system wide.

# Exit window on 15 min idle time
TMOUT=900
export TMOUT
Marty Metras
Super Advisor

Re: kill idle user

Attached is the script I use.
I have an exception list I look at before killing the user if idle. Idle is over 29 minuts. Over idle at over 39 minutes. And kill when over 59 minutes. Except if the user is on the list. I al so log the idle and kills.
Been using this for years.

idle_exception.list contains userID folloewd by a + sign like "metm+"

This may help you.
Marty
The only thing that always remain the same are the changes.
peterchu
Super Advisor

Re: kill idle user

thx reply ,

I hv tried Sridhar Bhaskarla 's script ,

but have the below error when run it , could suggest how can I modify it ? thx.
Muthukumar_5
Honored Contributor

Re: kill idle user

Olypian missed to complete while do done there.

Try that as,

#!/usr/bin/sh

PATH=$PATH:/usr/bin:/usr/sbin:/usr/contrib/bin

TIMEOUT=120
EXCEPTIONLIST=/etc/exception.lst
touch $EXCEPTIONLIST

kill_pid()
{
PID=$1
kill -9 $PID
}

echo "User:Activity:PID:Status"
who -u | while read line
do
time=$(echo $line | awk '{print $6}')
pid=$(echo $line | awk '{print $7}')
user=$(echo $line | awk '{print $1}')

grep -q "^${user}:" $EXCEPTIONLIST
if [ $? != 0 ]
then
if [ "$time" = "old" ]
then
(( time = $TIMEOUT + 1 ))
else
echo $time |grep -q "\."
if [ $? != 0 ]
then
time=$(echo $time|awk '{FS=":";print $1*60+$2}')
else
time=0
fi
fi
fi


if [ $time -gt $TIMEOUT ]
then
echo "$user:$time:$pid:Killed"

# Uncomment to do kill that PID
#kill_pid $pid
else
echo "$user:$time:$pid:Untouched"
fi
done

HTH.

Regards
Muthu.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: kill idle user

Sorry sridhar, I missed m on Olympian there. :((.

Peter,

I have assigned points to 514 of 619 responses to my questions. Assign else more there. ;)

PS: 0 points to this.
Easy to suggest when don't know about the problem!
peterchu
Super Advisor

Re: kill idle user

thx Muthukumar ,

I tried your script , it work fine at UNIX system , it fully meet the requirement , however , I also want to run it at linux , if I run it at RH linux , it will kill all the users right now ( even the user is processing sth ) , could suggest how can make it to make at linux ? thx.
Muthukumar_5
Honored Contributor

Re: kill idle user

Peter,

Use this script to on RH Linux versions. IT is working.

#!/bin/sh
# RH Linux idle user kill

# PATH
PATH=$PATH:/bin:/usr/bin:/usr/sbin:/usr/contrib/bin

TIMEOUT=120
EXCEPTIONLIST=./exception.lst

touch $EXCEPTIONLIST

if [[ ! -s $EXCEPTIONLIST ]]
then
echo "$EXCEPTIONLIST File is available with no users to avoid killing"
fi

kill_pid()
{
PID=$1
kill -9 $PID
}

echo "User:Activity:PID:Status"
who -u | while read line
do
time=$(echo $line | awk '{print $6}')
pid=$(echo $line | awk '{print $7}')
user=$(echo $line | awk '{print $1}')

grep -q "^${user}:" $EXCEPTIONLIST
if [ $? != 0 ]
then
if [ "$time" = "old" ]
then
(( time = $TIMEOUT + 1 ))
else
echo $time |grep -q "\."
if [ $? != 0 ]
then
time=$(echo $time|awk '{FS=":";print $1*60+$2}')
else
time=0
fi
fi
fi

if [ $time -gt $TIMEOUT ]
then
echo "$user:$time:$pid:Killed"

# Uncomment to do kill that PID
#kill_pid $pid
else
echo "$user:$time:$pid:Untouched"
fi

done

# Normal exit
exit 0

###

HTH.
Easy to suggest when don't know about the problem!
peterchu
Super Advisor

Re: kill idle user

very thx for your contiuouos help

But I still have error when run it , could suggest what can I do ? thx.

kill_user: line 38: [: 175:: integer expression expected
edp_usr:175::12650:Untouched
Sridhar Bhaskarla
Honored Contributor

Re: kill idle user

Peter,

I don't have a Linux system to test it. Can you post the output of 'who -u' on your linux box?. Obviously who -u is not giving the output in the format the script is excepted to.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
peterchu
Super Advisor

Re: kill idle user

thx ,

the who -u output is

edp_usrpts/13 Oct 28 14:30 . 7842 (192.168.1.12)

the output is the same as unix , thx
peterchu
Super Advisor

Re: kill idle user

the who -u output of unix is the same as linux , is it caused by the shell difference ? thx.
peterchu
Super Advisor

Re: kill idle user

do I need to install some shell tools ? could provide any suggestion ? thx