Logon profile kill script only partially working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logon profile kill script only partially working
# 1  
Old 08-19-2008
Logon profile kill script only partially working

I have been having an issue with the new motorola rf scan guns opening up too many sessions at once. It seems they will open a new connection for no reason, leaving the old one in the background and the user has no idea it is happening. To combat this, I have added the following code to the logon profile script that is supposed to see if the user's IP address is already logged on and if it is, it will kill the old session and let the new one on:

Code:
IP=$(who am i| awk '{print$6}')
IPCNT=$(who -u | grep $IP |wc -l)                                 
if [$IPCNT -ge 1 ]                                                
then                                                              
who -u | grep $IP |sed -e 's/://g' | awk '{print$6,$7,$8}' > idle 
cat idle | sort -k6 | awk '{print$2}' > idlegood                  
pts=$(who am i|awk '{print$2}')                                   
newpid=$(who -u | grep "$pts " | awk '{print$7}')                 
cat idlegood | grep -v "$newpid" > oldpids                        
while read PID; do kill -9 $PID; done < oldpids                   
fi

after that part it executes the main program all the users run.

It seems to work but every now and then one of the user accounts that uses this profile script will open up another session anyway.

As you can see, I am a beginner and I am sure there is a much more elegant way to do this, but I am still learning. Can anyone tell me why it could be that it works for most users but not others?

Thanks
# 2  
Old 08-21-2008
Not bad for a beginner. I'm surprised the line with the "if" isn't a problem, however. You need a space between [ and $IPCNT.

Second, it's better to do a kill -1 $PID than kill -9. The former will try to close the child processes of a shell, while the latter may result in hung child processes. Better yet, don't just kill the process, but kill the process group. This is the second field from "ps -j $PID". So you can do:
Code:
while read PID; do 
 GPID=$( ps -j $PID | awk 'NR==2 { print $2 }' )
 kill -1 -$GPID
done < oldpids

It might also be necessary to wait for two seconds before continuing the script, giving the system a chance to perform the kills before the script continues. Adding "sleep 1" I think might be enough.

Also, you don't have to do a cat every time -- grep, sed, awk can all take a filename as an argument -- but maybe using cat helps you keep things straight. Also, no need to do a grep and then an awk. Awk does both. (sed -n works similarly.)

My system's "whoami" doesn't work the same as yours, otherwise I could test a similar script. But fix the problem with the bracket
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command / script to partially rename file

Hi I have numerous files names product_host_result_B1000842.txt product_host_result_B1000847.txt product_host_result_C1000842.txt product_host_result_C1000848.txt etc. I need them renamed so that the 'product_host_result' becomes 'output_product_host' but the rest of the filename is... (6 Replies)
Discussion started by: Grueben
6 Replies

2. UNIX for Dummies Questions & Answers

Script partially executes??

Hi All, I am calling a shell script from another shell script, however, it only executes part of it (the echo commands only). What could be some causes for that? For example: ShellScriptA.sh: ... ... ... . ShellScriptB.sh ShellScriptB.sh contents: echo date echo... (7 Replies)
Discussion started by: DBnixUser
7 Replies

3. Shell Programming and Scripting

kill -9 not working

Hi All, I tried killing a bunch of scripts. They seem to have worked because ps -ef | grep -i SCRIPT_NAME is not showing them but the logs are getting populated and jobs in the script are getting done. How do I really kill them or atleast see the process running? Thanks Sumeet (12 Replies)
Discussion started by: sumeet
12 Replies

4. UNIX for Advanced & Expert Users

user .profile file not working

Hi All, I tried creating a .profile file but it is not working even after exiting the session. But before that I will give a quick background. I log into UNIX(SUN - ksh) and then SUDO to another user. After that I change DIRECTORY. In this DIRECTORY I tried creating the profile file but it... (4 Replies)
Discussion started by: sumeet
4 Replies

5. Solaris

After giving Bash the .profile settings are not working

I am using Solaris ... I have some settings in my .profile and in .profile iam calling some scripts. After i logged in to that box if i gave "bash" and proceed with my shorcuts(alias) are not working . (3 Replies)
Discussion started by: girija
3 Replies

6. AIX

Kill -9 not working?

What to do when Killl -9 doesn't work to end a process? (2 Replies)
Discussion started by: bbbngowc
2 Replies

7. AIX

AIX 5.3 ML10 new install, rsh only partially working

Two boxes we just reloaded to use as a two node HACMP cluster to insure our software is compatible. Install disks we received at our library on 5/2009 AIX 5.3-ML10 For sake of argument names ibmaix1 and ibmaix2 On both: ibmaix1# oslevel -r 5300-10 .rhosts files with 600 permissions... (0 Replies)
Discussion started by: DrKillPatient
0 Replies

8. Solaris

/etc/profile file edited. No command working

I have X4500 and I created a user. I wanted to give him root privileges and for editing the sudoers files I typed visudo sudoers. But it said visudo command not found. After googling I found that we need to set path in etc/profile. I edited that and put the following command ... (3 Replies)
Discussion started by: bharu_sri
3 Replies

9. UNIX for Advanced & Expert Users

commands after .profile modification not working

Hi , I have modified .profile and after that i ran it as .profile. Now no command i working even ls is not working When i type ls , it is showing that -ksh not found I tried to run as . $home/.profile , it is saying as permission denied . How can we do that .. any idea.. Thanks... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

10. UNIX for Advanced & Expert Users

Kill - not working

Hi, I have logged into a Sun Solaris machine( Putty -telnet ) , which got hung. now I need to kill the process - ksh When I tried to kill like : kill -9 <pid of ksh> , it is not giving any error , but the process is not getting killed... Here is what I did $ps -fu shihab ... (5 Replies)
Discussion started by: shihabvk
5 Replies
Login or Register to Ask a Question