1847986 Members
6985 Online
104022 Solutions
New Discussion

Re: how to kill users

 
Rajesh_47
New Member

how to kill users

Hi,

Our ERP clients connects server using telnet over the WAN. If in between connection drops user session hungs inbetween.

How can I schedule that system should check the users who are inactive from last 30 or more minutes and kill that ID's automatically.

HP Unix version is 11i.
10 REPLIES 10
Bharat Katkar
Honored Contributor

Re: how to kill users

You can write a scirpt for this..i can give some guide lines for the same.
1. # w
this output will give who all are logged in.
2. # cut
Use this command to get Value of Idle field from the ouput
3. Repeat same to get ttty no of the same.
4. # ps -t
This will list all the processes fo that user with PID
5. # kill -9
Kill all processes using this along with shell process for that particular user and your user is logged out.

Hope this helps you
You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: how to kill users

I forgot one last thing about scheduling.
# crontab -e
Use this command to put your shell script in CRON for scheduling its execution.

see man cut, man ps, man contab if required.

You need to know a lot to actually know how little you know
Scott Van Kalken
Esteemed Contributor

Re: how to kill users

you should probably just use a kill as opposed to a kill -9. Keep track of those you can't get rid of with a kill and then use a kill -9.

hein coulier
Frequent Advisor

Re: how to kill users

"how to kill users" !?

Maybe you're in the wrong forum ;-)

Perhaps you can set TMOUT=1800 in /etc/profile (look in manpage of sh or ksh).
Robert True
Frequent Advisor

Re: how to kill users

You can add the var TMOUT to the users .profile (or the /etc/profile for system wide effect) and set to the # of seconds the usr may remain idle before being logged out: ie

TMOUT=300 # Time out in 5 minutes
export TMOUT

Unfortunatlly, I think this only works if the user is truly idle. If they are running a menu program or editor or some such thing, even though they may not be doing any I/O, I don't think they will be logged out, but I could be wrong.
Bill Hassell
Honored Contributor

Re: how to kill users

As mentioned, kill -9 is a very bad thing to do. This is especially true for processes that keep multiple open files and the data in the files must be synchronized. kill -9 must be a *LAST* resort. Use kill -15 (or just plain kill). Note that you must kill the top-level parent, typically the login shell. From the w command, you will see the tty device so you can find all the processes associated with a specific login by using:

ps -ft pts/ta

where pts/ta (or perhaps ttyr3) is the tty device mentioned in w. The login shell will always end with a - as in: sh- or ksh- and that is the process to kill. If you use w -l, then you'll get the top-level process as the last field. To extract the last process name, you'll have to use awk's NF value since the number of fields in w variesd because of null information.

Be very careful with testing your shell script: always exclude root since you may kill an important process someone is running, and also be careful that a particular process that someone may be running should not be killed...that is application dependent.

As mentioned, TMOUT can be used to expire the shell, but only is the shell is running. If a user runs a process like vi, vi will never terminate if the user walks away and the shell will never run.


Bill Hassell, sysadmin
Tapas Jha
Valued Contributor

Re: how to kill users

Hi,

If you are using database then you must first kill the process involved in that. First kill from database end and then kill from unix end. Though kill -9 is not recommended first, you first choose -15 option and then -9. Here is the script which can help you.This scrip will kill users who are idle since 30 to 59 minutes. Keep it in cron or run manually.

who -u |grep query| cut -c 1-10,39-50| grep -e "[0]:[30-59]" > current
for i in `cat current | awk '{print
$3}'`
do
kill -15 $i
kill -9 $i
$i
done
Rgds
Tapas
Tapas Jha
Rory R Hammond
Trusted Contributor

Re: how to kill users

I have long used a c program that walks the tree and trys nice kills waits a short time then tries a meaner kill until the -9


But I have thought that is worth discussing.
who -tu has the following output:
rory pts/3 Apr 30 08:41 1:33 18310 rrhammond:0.0

fuser -uk /dev/pts/3

kills all of the process associated with that login.


There are a 100 ways to do things and 97 of them are right
Keith Bevan_1
Trusted Contributor

Re: how to kill users

Rajesh,

KIll -9 pid is vary bad practice, and will certainly leave accounting connections open if you use the w command.

Kill -15 pid is the better option and more tidy!

TMOUT in the profile sounds good for shell sessions but not always practical for db applications that may want the db connection closing in a more desirable method.

The best method for accuracy & control is to perform the kill manually, unfortunately this can be laborious and not the answer you are looking for.

Keith
You are either part of the solution or part of the problem
Rajesh_47
New Member

Re: how to kill users

Dear All,

Thanx for your response to my query. I got the perfect ans for my query. I want to share it with you. I am using the following command to kill users who are ideal.

/usr/bin/who -u | /usr/bin/awk '{print $6" "$7}' | /usr/bin/grep 0:[345] | /usr/bin/awk '{print "/usr/bin/kill -9 "$2}' | usr/bin/sh

Regards

Rajesh