User Login Limit

Gud day :slight_smile:

We have a limited user login so we want to restrict 1 login per user. We have added below script in each user's profile but it is not working :confused: , I displayed the output for COUNT (by inserting echo command) but the value is always 1. Hope you could help me.

Thanks :wink:

IAM=`who am i | cut -d" " -f1`
COUNT=`w | cut -d" " -f1 | grep "^$IAM$" | wc -l`
[ $COUNT -gt 1 ] && exit 0

Try using output from the last command

 last | grep jmc | head
jmcnama  pts/16       Tue Jun  7 08:14   still logged in
jmcnama  pts/7        Mon Jun  6 11:18 - 19:05  (07:46)
jmcnama  pts/7        Mon Jun  6 08:14 - 11:18  (03:03)
jmcnama  pts/3        Fri Jun  3 07:41 - 20:15  (12:33)
jmcnama  pts/16       Thu Jun  2 08:17 - 16:27  (08:09)
jmcnama  pts/11       Wed Jun  1 07:42 - 20:15  (12:32)
jmcnama  pts/3        Fri May 27 07:31 - 14:44  (07:12)
jmcnama  pts/30       Thu May 26 08:26 - 20:39  (12:12)
jmcnama  pts/21       Wed May 25 08:28 - 20:12  (11:43)
jmcnama  pts/38       Tue May 24 13:04 - 13:11  (00:07)

You can detect multiple logins with something like this:

login_count= `last | grep $USER | awk '
        BEGIN{count=0} { if (index($0,"still logged")>0) { count++}} END{print count}'`                                 

Here is what I do :

name=`echo $LOGNAME,`
sessactive=`who | awk '{printf",%s,\n",$1}' | grep ,$LOGNAME, | wc -l`

And then you'll know how many session that user has...and I guess you know what to do next.

I have change my code a month ago because that did not give me the correct count. Sometime users had some other jobs running as phantom but no session open, we have a Universe database and I had to modify my code such as :

 
sessactive=`ps -ef | grep /uv/bin/uv | awk '{printf",%s,\n",$1}' | grep ,$LOGNAME, | wc -l`

Now it is working better.
Hope it helps.

hi ~*

The script worked perfectly fine.
thanks for the help :smiley: