LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Writing a script to kill users logged in twice (https://www.linuxquestions.org/questions/programming-9/writing-a-script-to-kill-users-logged-in-twice-590591/)

scrizo 10-09-2007 01:52 PM

Writing a script to kill users logged in twice
 
I'd like to know the best approach at writing a script that will kill users who are logged in twice.

We use linux for our business and the users dont get access to a shell instead they login and go straight to the program. However if for some unforseen instance they lose connection, when they login and go to the program they cant update anything because the process is in use. Is there a way to kill the double logged users more likely by timestamp so I know 8:00 logged in once and 12:00pm i know to kill the 8:00 one because thats the first one. Man I hope I didnt confuse anyone with my question.

unSpawn 10-09-2007 04:45 PM

Can't you use "screen" to set up the session to the app? In that case on disconnection they can reconnect to their session?

rsashok 10-09-2007 07:48 PM

You might want to consider to examine the output of 'ps' command. For example if you do:
> ps -Af | grep "pts\/"
You might see 'double' users. I'm sure that there is an option in 'ps' to see start time of the process.

chrism01 10-10-2007 06:29 AM

ps -f|grep <username>
5th field is login time

bigearsbilly 10-10-2007 07:39 AM

sounds harsh, I'd avoid it logging things off summarily like that.

how do they log in? can you not use exec to run the program?
or put the program as the shell in the passwd file?

or kill all users at midnight?

matthewg42 10-10-2007 08:37 AM

There are also the w and who commands.

You should probably only kill login shells with a long idle time. And you should be prepared to get a lot of hate mail from your users.

scrizo 10-10-2007 09:04 AM

Quote:

Originally Posted by bigearsbilly (Post 2919582)
sounds harsh, I'd avoid it logging things off summarily like that.

how do they log in? can you not use exec to run the program?
or put the program as the shell in the passwd file?

or kill all users at midnight?

In the passwd file is the program as there shell. I need to kill them because when they get disconnected or are logged out and get back in the application they cannot do any updates because the pid is locked to there session still preventing them from making updates and then I have to manually kill the pid of the users so they can get back in the account.

schneidz 10-10-2007 12:01 PM

hi, here is my quick-and-dirty solution.
this works in aix - not sure what all the ps options do:
Code:

# use who to determine who is logged on more than once:
dupe=`who | awk '{print $1}' | sort | uniq -d`

for item in `echo $dupe`
do
 echo dupe = $item
 kill -9 `ps -ANaedfklm | grep $item | grep ksh | grep -v grep | head -n 1 | awk '{print $4}'`
done

you would need to cron this every minute or every day...
my experirience is that ps displays the most recent login on top so you may need to use tail instead of head.
you may need to substitute 'ksh' with whatever program they are running at login.


All times are GMT -5. The time now is 08:56 PM.