1840488 Members
4659 Online
110165 Solutions
New Discussion

Re: Script wanted

 
SOLVED
Go to solution
peterchu
Super Advisor

Script wanted

There are many users login to our system but do nth on it , then many users can't log into it as we have limit the no. of login, I would like to kill the user login that are idle for over 30 minutes ( but except a specific group of user like EDP user ) , do anyone have the similiar script , if yes , could post to there ? thx.
14 REPLIES 14
Manish Srivastava
Trusted Contributor
Solution

Re: Script wanted

Hi,

You could do a finger(1) and take the Idle time and the project of the person logged in to the system and then terminate sessions based on your needs.

Regards
Sergejs Svitnevs
Honored Contributor

Re: Script wanted

Setting TMOUT in .profile file will disconnect login session after a period of inactivity.

TMOUT=1800


Regards,
Sergejs
Tapas Jha
Valued Contributor

Re: Script wanted

Hi,

Below script will kill those users who are idle between 31 to 59 minutes. Save it and run this. You can run it from cron.

!/usr/bin/sh
who -u |grep -v EDI| cut -c 1-10,39-50| grep -e ":[3-5][1-9]" > current
for i in `cat current | awk '{print $3}'`
do
kill -9 $i
done


Regards
Tapa
Tapas Jha
Mark Grant
Honored Contributor

Re: Script wanted

Personally, I'd really worry about using "kill -9" on anything that "who" had anything to do with. One corrupted *tmp file and you could be killing "swapper" or "init".

I'd suggest either putting in lots of checks to make sure you're killing what you think you are killing or, as mentioned above, use "TMOUT".
Never preceed any demonstration with anything more predictive than "watch this"
Victor Fridyev
Honored Contributor

Re: Script wanted

Hi,

Sorry, but I'm not sure that this is correct to kill users' sessions, even if the sessions are not active. If this doesn't overload computer, let them live, change the kernel parametres, which limit number of users and forget about the problem.

But if you want to do this in any case, so
who -T | sed 's/://' |
awk '$7=="old"||$7>30 {system("kill -9 " $(NF-1)}'

Good luck and prepare to talk with angry users.
Entities are not to be multiplied beyond necessity - RTFM
Hein van den Heuvel
Honored Contributor

Re: Script wanted


I am with Victor,

IMHO you will spend much more time and resources hunting down idle session, checking them, killing them, then you'll ever save by not having them.
And any user reconnecting subsequently, will blow away the 'gains', not to mention the aggravation caused.

Idle process killers are only usefull if there is a serious non-replennishable resource to be protected. In the old days that used to be MODEM lines.

Nowadays, just increase nproc, network ports and the likes!

If you do feel you need to have an idle process killer (maybe because you are really-really tight on memory and swap, or maybe to give a (false!) sense of security towards management) then set the timeout to a day or two please!

Cheers,
Hein.

rmueller58
Valued Contributor

Re: Script wanted

When we first brought our system on line we were having problem with # of ttys, even though we had 250 licenses, we were stopped at 60 logins. You may want to verify the correct # of ttys are available as well.

When we were running into problems we bumped "inactive" users after 2 hours and gad you'd think we were Saddam's sons putting cigarettes in peoples eyes.

Our biggest call volume on the helpdesk is when people can't get into the system.. We got the tty issue fixed quickly.
Make sure your licensing # is enough to handle a consistent load.

For a while you may want to run the script below to log the usercount and watch your peaks and valleys. It helped us reduce licensing from 250 to 100. Cutting our yearly support fee by boocoo bucks.

#ROOT CRONTAB
* 4-22 * * * /usr/local/bin/monitor/usercount
55 5 * * * /usr/local/bin/monitor/dailycount


# more usercount
export whoson=`who|wc -l`
# echo $whoson `date +%D` `date +%T` >>/tmp/usercount.log

if [ "$whoson" = [0-9] ]
then
echo "0$whoson" `date +%D` `date +%T` >>/tmp/usercount.log
else
echo "$whoson" `date +%D` `date +%T` >> /tmp/usercount.log
fi
# more dailycount
sort -n /tmp/usercount.log |tail -1 >> /tmp/dailycount.log
mv /tmp/usercount.log /home/backup/monitor/usercount`date +%m%d%y`.log


Just a thought or two..
peterchu
Super Advisor

Re: Script wanted

thx replies, but I really want to kill the idle user with a script ,

I tried the script , it is Ok , but if I want the EDP users can't be killed by the script , what can I do ? thx.
peterchu
Super Advisor

Re: Script wanted

thx Tapas Jha ,

I tried your script , it is work , but if I want to find the idle time over 60 minutes , how to do it ? thx.
Bharat Katkar
Honored Contributor

Re: Script wanted

PeterChu,
It's an good idea to use TMOUT variable in the .profile file of users except which belongs to EDP user. That way it would be easy and harmless.
Regards,
You need to know a lot to actually know how little you know
peterchu
Super Advisor

Re: Script wanted

thx reply , use TMOUT is good , but we have over 500 users , I don't want to update all the user profile , use the script is good for me .
Mark Grant
Honored Contributor

Re: Script wanted

You don't have to put it in every users profile.

Put it in /etc/profile and everyone gets it.
Never preceed any demonstration with anything more predictive than "watch this"
peterchu
Super Advisor

Re: Script wanted

thx Mark,

I test TMOUT is fine , but if I want it only apply to non EDP user , who can I do ? thx
Mark Grant
Honored Contributor

Re: Script wanted

IN /etc/profile you could do something like

case $LOGNAME in
EDP*)
TMOUT=1800
;;
esac
Never preceed any demonstration with anything more predictive than "watch this"