cancel
Showing results for 
Search instead for 
Did you mean: 

control login

 
elainelaw
Occasional Contributor

control login

I use linux box , in the system , all user are only allowed one login at the same time , that mean all users can only have one login session in the system at the same time , so sometimes when the user was disconnected by unexpected reason (the reason such as unstable network ) , then the system does not allow the user login again until the pervious login was logged out , so I would like to ask how to do that - when the user login, if it found there is a pervious login session in the system ( or this user's process ) , then logout this login and kill all its process , so that the system release the login to user , then the user can login to the system now , can advise how to do that ? thx in advance.
2 REPLIES
Ivan Ferreira
Honored Contributor

Re: control login

You must consider that if you don't handle this carefully, the user may "forget" that was already logged in and try a second session, killing all its jobs. That is not good.

You will need a script like this in the profile:

SESCOUNT=`who -a | grep $USER | wc -l`
if [ $SESCOUNT -gt 1 ]
then
THISTTY=`tty | sed "s/\/dev\///"`
OLDSHELL=`ps -u $USER | grep -v $THISTTY | awk {'print $1}'`
kill -9 $OLDSHELL
fi

What it does is to check all "bash" process and kill the process not asociated with the current terminal. If you kill the parent shell, all children processes will die.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Steven E. Protter
Exalted Contributor

Re: control login

Shalom,

There is nothing built in to Linux to perform this. You have to use the code that Ivan posted or soemthing else.

/etc/profile is the gate where you can do this kind of conditional access.

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