Ksh script function, how to "EXIT 2" without killing the current process?

Hi,

Using AIX 5.3 and Ksh.

 
/>ls -al /usr/bin/ksh
-r-xr-xr-x 5 bin bin 237420 Apr 10 2007 /usr/bin/ksh
/>

I recently started working for a new employer. I have written UNIX K-Shell scripts for many years and have never had this particular issue before. Its perplexing me.

I have the following script:

#
# KillBadMonitorDaemons.sh
#
# Searches for duplicate processes and echoes findings
#
 
function  killbadmonitordaemons {
echo "Searching for rogue Monitor Daemon processes...\n"
for mysite in `cat /qdxtest/qdx5.4/integrator/prodsites`
do
 
  # echo and setsite to check
  echo "Checking site: $mysite"
  #setsite $mysite   #; purposely commented to make it fail and hit "exit" command.
 
  # Verify the setsite command was successful
  sitecheck=`showroot | grep "HCI site" | awk '{print $4}'`
  if [[ $sitecheck != $mysite ]]
  then
    echo "Setsite Command was unsuccessfull. Aborting Script.\n"
    exit 2
  fi
 
  #echo "Getting pid from hcisitectl command"
  goodpid_sitectl=`hcisitectl | grep hcimonitord | awk '{print $6}'`
 
  #echo "Getting pid from file HciSiteDir/exec/hcilockmgr/pid"
  goodpid_pidfile=`cat $HCISITEDIR/exec/hcimonitord/pid`
  #echo "Making sure the two pids match. If not, don't do anything for this site."
  if [[ $goodpid_sitectl != $goodpid_pidfile ]]
  then
 
     echo "** pid file and hcisitectl do not match for $mysite.\nSkipping $mysite.\n"
 
  else
    #echo "Good Monitor Daemon for site $mysite is running on pid: $goodpid_sitectl"
    #echo "Grepping for bad Monitor Daemon pids for site $mysite"
    for mypid in `ps -ef | grep "$mysite " | grep hcimonitord | grep -v $goodpid_sitectl | awk '{print $2}'`
    do
 
      #echo `showroot`
       echo "$mysite: Found rogue Monitor Daemon running on pid $mypid"
       echo "`ps -ef |grep "$mysite " | grep hcimonitord`\n"
       #kill -9 $mypid
       sleep 3
 
    done
 
  fi
 
done
echo "\n*** Done searching ***"
}
# execute main function
killbadmonitordaemons
unset mysite goodpid_pidfile goodpid_sitectl mypid sitecheck

When I execute this script from the command line list this:

 
/>pwd
/qdxtest/qdx5.4/integrator/troytest
/>KillBadMonitorDaemons.sh

the EXIT command ends my current Telnet session.

So, I added a line at the top:

 
#!/usr/bin/ksh

It still ends my Telnet session even though (I thought) #!/usr/bin/ksh should be telling the script to execute in a new shell process. RIGHT?

So, I tried changing the line at the bottom which calls my function to fork a new proces.

 
echo `killbadmonitordaemons`

Then when I run my script again...

 
/>pwd
/qdxtest/qdx5.4/integrator/troytest
/>KillBadMonitorDaemons.sh
Searching for rogue Monitor Daemon processes... Checking site: tds Setsite Command was unsuccessfull. Aborting Script.
ksh[59]: KillBadMonitorDaemons.sh:  not found
/>

PATH Environment Variable:

/>echo $PATH
/qdxtest/qdx5.4/integrator/troytest/bin:
/qdxtest/qdx5.4/integrator/troytest/scripts:
/qdxtest/qdx5.4/integrator/bin:
/qdxtest/qdx5.4/integrator/contrib:
/qdxtest/qdx5.4/integrator/sbin:
/qdxtest/qdx5.4/integrator/dbms/bin:
/qdxtest/qdx5.4/integrator/tcl/bin:
/qdxtest/qdx5.4/integrator/clgui/bin:
/qdxtest/qdx5.4/integrator/clgui/java/bin:
/qdxtest/qdx5.4/integrator/usercmds:
/usr/bin:/etc:/usr/sbin:/usr/ucb:
/home/hci/bin:/usr/bin/X11:/sbin:
.:
/usr/local/bin:/usr/local/scripts:
/opt/pware/samba/3.0.23d/bin

Directory listing where script resides:

/>pwd
/qdxtest/qdx5.4/integrator/kshlib
/>ls -al *.sh
-rwxrwxr--   1 hci      staff          1202 Oct  8 12:54 KillBadLockManagers.sh
-rwxrwxr--   1 hci      staff          1619 Oct  8 13:58 KillBadMonitorDaemons.sh
-rwxrwxr--   1 hci      staff          1263 Sep 24 16:08 monitorDbStates.sh
-rwxrwxr--   1 hci      staff          1479 Oct  2 15:27 showThreadQueues.sh
/> 

I appears my script directory isn't even in the PATH and yet I can execute the script from anywhere. I don't get it. My co-workers say it has always been this way.

Anyway, it appears that running the script like this is causing my "\n" to stop working. And, what is the "not found" message? Am I doing something wrong?

I've never had problems creating scripts which execute in a new shell and have the EXIT command work as expected without strange results.

Possibly, our shell is different or some OS setting is different than previous environments I've worked in?

Thanks for any assistance.

I think you may have a script by the same name somewhere in your path.
I suggest you do a find on your paths...

I renamed the script and there doesn't appear to be any other script by this name in my path.

/>KillBadMonitorDaemons.sh
Searching for rogue Monitor Daemon processes... Checking site: tds Setsite Command was unsuccessfull. Aborting Script.
ksh[59]: KillBadMonitorDaemons.sh:  not found
/>
/>pwd
/qdxtest/qdx5.4/integrator/kshlib
/>ls -al *.sh
-rwxrwxr--   1 hci      staff          1523 Oct  8 15:14 KillBadLockManagers.sh
-rwxrwxr--   1 hci      staff          1624 Oct  8 15:19 KillBadMonitorDaemons.sh
-rwxrwxr--   1 hci      staff          1263 Sep 24 16:08 monitorDbStates.sh
-rwxrwxr--   1 hci      staff          1479 Oct  2 15:27 showThreadQueues.sh
/>mv KillBadMonitorDaemons.sh tmm_KillBadMonitorDaemons.sh
/>KillBadMonitorDaemons.sh
ksh: KillBadMonitorDaemons.sh:  not found
/>