1827871 Members
1914 Online
109969 Solutions
New Discussion

kill user

 
SOLVED
Go to solution
peterchu
Super Advisor

kill user

We have about 300 users on the Linux system , and due to the license is limit , we only allow 200 users logins at the same time , but the total of logins always over 200 , so I want to kill the users who idle for a specific time ( eg . 30 minuts ) , i tried to use TMOUT , but it seems not good as we have many users in the system , is there any script can kill the idle users ? thx in advance.
12 REPLIES 12
Steven E. Protter
Exalted Contributor

Re: kill user

TMOUT will not work on users with the application already open.

What you can do is process the output of an application monitor through a script. If you have an application monitor that will show idle users you can do this:

root su - to app owner user runs utility, makes a list of users to kill. End of script exits.

Then root processes the list with kill -9 commands.

To be more specific, I need to know the app.

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sridhar Bhaskarla
Honored Contributor
Solution

Re: kill user

Hi,

'w' will show the idle time as the 4th column for each user. You should be able to script yourself to kill the users the have more than 30 mins of idle time. You may have to spend sometime on it as you may have to convert days, hours, mins etc.,

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

Re: kill user

Hi,

try the following

for i in `who -u|awk '$6>30{print $7}'`
do
kill (-9) $i
done

This should kill all user procs which are idle for more than 30 minutes

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: kill user

We can control the user login based on the script as,

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

time=`echo $user | awk '{ print $1 }'`
pid=`echo $user | awk '{ print $2 }'`

if [[ $time -gt 30 ]]
then
echo "Login denied because of reaching user max. limit 300"
kill -9 $pid
fi
done

You can control this in 2 ways as,

1> Put this script in /etc/profile so that new user login is denied to log

2> Put this script in cron to automate in the correspoding timings.

Regards
Muthu



Easy to suggest when don't know about the problem!
Franky_1
Respected Contributor

Re: kill user

Hi,

if anyone could help you then please assign points

Regards

Franky
Don't worry be happy
peterchu
Super Advisor

Re: kill user

thx Muthukumar , I tried your script , it meet the requirement , but when I run it , it will display the below message , it seems can't kill the user yet , could suggest what is wrong ? thx.


# ./kill_user
./kill_user[5]: 90:27392: not found.
./kill_user[5]: 407:17966: not found.
./kill_user[5]: 209:21212: not found.
./kill_user[5]: 114:18946: not found.
./kill_user[5]: 38:21098: not found.
./kill_user[5]: 38:21119: not found.
./kill_user[5]: 379:22933: not found.
./kill_user[5]: 158:21181: not found.
./kill_user[5]: 455:21627: not found.
./kill_user[5]: 60:22365: not found.
./kill_user[5]: 24:25634: not found.
./kill_user[5]: 11:28360: not found.
Elmar P. Kolkman
Honored Contributor

Re: kill user

It seems the two awk lines for user and pid are missing the '-F :' arguments to set the field seperator.
Every problem has at least one solution. Only some solutions are harder to find.
peterchu
Super Advisor

Re: kill user

thx replies, but I am not familiar with the script writing , I tried to modify the script ( add -F : )as below , but still hv error when run the script , please help to point out what I am wrong , thx in advance.


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


########### Error #################

real 0.0
user 0.0
sys 0.0
./aa[5]: pid: not found.
No such file or directory: =

real 0.0
user 0.0
sys 0.0
./aa[5]: pid: not found.
No such file or directory: =

real 0.0
user 0.0
sys 0.0
./aa[5]: pid: not found.
Muthukumar_5
Honored Contributor

Re: kill user

Hai Peter,

Check this one,
#!/usr/bin/ksh
#killuser
set -x
for user in `who -u | awk '{ print $6":"$7 }' | grep -v "\."| awk -F : '{ print ($1*60)+$2":"$3 }'`
do

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

if [[ $time -gt 30 ]]
then
echo "Login denied because of reaching user max. limit 300"
kill -9 $pid
fi
done

-----------

# ksh killuser
It will give the debugging statements.

Problem may be because of only -F :

But


eal 0.0
user 0.0
sys 0.0
./aa[5]: pid: not found.
No such file or directory: =

real 0.0
user 0.0
sys 0.0
./aa[5]: pid: not found.
No such file or directory: =


say -->

Did you change script contents or execute with time command ?!?

It is good to have to know what did u try on that. and give the debug mode output.

Note: I have test it is successfull doing
Easy to suggest when don't know about the problem!
Ermin Borovac
Honored Contributor

Re: kill user

Here is another way with perl. The script should print ps output of processes it would terminate. Examine the output first and if ok, uncomment line with /usr/bin/kill. Script ignores idle CDE sessions, console logins and root processes.

#!/usr/bin/perl

# Idle limit in minutes
$idle_lim = 30;

@w = `/usr/bin/w -h`;
chomp @w;

$w1 = 29;
$w2 = 22;

for (@w) {
($a, $b, $what) = /^(.{$w1})(.{$w2})(.*)$/;
($user, $tty, $login_at) = split(" ", $a);
($idle, $jcpu, $pcpu) = split(" ", $b);
if ($idle =~ /:/) {
($idle_h, $idle_m) = split(/:/, $idle);
$idle = $idle_h * 60 + $idle_m;
}
if ($idle > $idle_lim and $tty =~ /^[pt]/ and $user ne "root") {
print "$user at $tty idle for $idle minutes\n";
@pids = `UNIX95= /usr/bin/ps -t $tty -o pid=`;
chomp @pids;
for $pid (@pids) {
system("/usr/bin/ps -fp $pid | grep -v PID");
# system("/usr/bin/kill $pid");
}
}
}
peterchu
Super Advisor

Re: kill user

thx replies,

hi Muthukumar ,

I tried your script , it work fine , but if I want to have one more requiremnet , I want to ignore to kill a list of users ( eg. senior staffs , EDP staffs and some special users ) , what can I do ? thanks in advance.
Muthukumar_5
Honored Contributor

Re: kill user

Hai,

We can sepate special users with the following script as,

#!/usr/bin/ksh
# killuser.ksh
set -x

# Enter the specific users
SPL_USER=""

######### Script starts now ##########
set -A USER_ARR $SPL_USER

set -A USER_NAME $(who -R | awk '{ print $1 }')
set -A PID_ARR $(who -Ru | awk '{ print $7 }')
set -A IP_ARR $(who -Ru | awk '{ print $8 }')
set -A TIME_ARR $(who -Ru | awk '{ print $6 }' | grep -v "\."| awk -F : '{ print ($1*60)+$2 }')

#######################################

index=0

while [[ $index -lt ${#USER_NAME[*]} ]]
do

loop=0
while [[ $loop -lt ${#SPL_USER[*]} ]]
do

if [[ "${SPL_USER[$loop]}" != "${USER_NAME[*]}" ]]
then

if [[ ${TIME_ARR[$index]} -gt 30 ]]
then

echo "Warning: Login denied because of reaching user max. limit 300"
# More contact informations in echo statements as your mail-ID too
kill -9 ${PID_ARR[$index]}

# You can add one more functionality as, make a mail
# mailx -s System usage ${USER_NAME}@yourdomain.com messages

fi

fi

let loop=loop+1
done

let index=index+1
done


where you have to fill in the script as,
# Enter the specific users
SPL_USER="test adm muthu"

space is needed between users.

Try to test it with out kill then start to use with kill command.

If you need more enhancement let me know.

HTH.

And Lot of responder(s) replied questions. So give reward by assigning points :-)

Regards
Muthu
Easy to suggest when don't know about the problem!