Francesco
15th October 2002, 00:10
The question on how to properly kill a session (or user) in Baan returns quite frequently on the board.

Here is a script that I use when all else fails, courtesy of Mike King.

Please note that this is written for Solaris and tools6.2.
It will work on nearly any system/Baan configuration, but some modifications might be required.


#!/bin/ksh
# **************************************************************************
# * File : killbaansess.ksh
# * Usage : killbaansess.ksh <pid of bshell6.2>
# * Purpose : This script is designed to kill all processes for a given
# * bshell pid, which is passed as the first parameter.
# *
# * The script first confirms that the provided PID is indeed
# * a bshell process. Once this is confirmed, it will locate
# * the parent id (should be bshell?????6.2). It will then
# * locate all the children of this process, so as to be
# * able to kill any orphaned processes
# *
# * We first try to kill it with the bshcmd6.2 command (Baan
# * provided utility). If this is successful in shutting
# * down the process, we are done.
# *
# * If it does not die, we must then kill each of the child
# * processes. We try with a kill -15, and then if that fails
# * we follow up with a kill -9.
# *
# * Author : Michael King
# * Date : 2001/05/10
# *
# * Mods : 2002/01/12 Francesco Frentrop - modified bshcmd command
# **************************************************************************
if [ $# -ne 1 ] ;then
echo '*********************************************'
echo ' Usage killbaansess <bshell pid>'
echo '*********************************************'
exit
fi

BSHELL_PID=${1}

# **************************************************************************
# * Confirm that this is indeed the bshell6.2 process
# **************************************************************************
if [ -z "$(ps -f -o args -p ${BSHELL_PID}| grep bshell6\.2)" ] ;then
echo '****************************************************************'
echo " PID ${BSHELL_PID} is not a bshell6.2 process"
echo '****************************************************************'
exit
fi

# **************************************************************************
# * Get the parent and grand-parent (in case of a ba6.2 as grandparent
# **************************************************************************
PPID=$(ps -f -o ppid -p ${BSHELL_PID}|grep -v PID)
gpid=$(ps -f -o ppid -p ${PPID}| grep -v PID)
if [ -z "$(ps -f -o args -p ${gpid}| grep ba6\.2)" ] ;then
GRANDPARENT=""
else
GRANDPARENT=$(ps -f -o ppid -p ${PPID}| grep -v PID)
fi

# **************************************************************************
# * Build a list of processes
# **************************************************************************
ps -ef -o pid -o ppid -o args > /tmp/$$.fulllist.dat
ps -ef -o pid -o ppid -o args| grep sh_server | grep -v grep > /tmp/$$.sh_server.dat
ps -ef -o pid -o ppid -o args| grep oracle8 | grep -v grep > /tmp/$$.oracle8.dat

# **************************************************************************
# * Find all of the related processes
# **************************************************************************
SH_PID=""
while read da_pid da_ppid args
do
if [ $da_ppid -eq ${BSHELL_PID} ] ;then
SH_PID=$da_pid
fi
done < /tmp/$$.sh_server.dat

SH_KIDS=""
if [ -n "$SH_PID" ] ;then
while read da_pid da_ppid args
do
if [ $da_ppid -eq ${SH_PID} ] ;then
SH_KIDS="$da_pid $SH_KIDS"
fi
done < /tmp/$$.fulllist.dat
fi

ORA8_PID=""
while read da_pid da_ppid args
do
if [ $da_ppid -eq ${BSHELL_PID} ] ;then
ORA8_PID=$da_pid
fi
done < /tmp/$$.oracle8.dat

ORA8_KIDS=""
if [ -n "$ORA8_PID" ] ;then
while read da_pid da_ppid args
do
if [ $da_ppid -eq ${ORA8_PID} ] ;then
ORA8_KIDS="$da_pid $ORA8_KIDS"
fi
done < /tmp/$$.fulllist.dat
fi

#echo '****************************************************************'
#echo Processes found
#echo BSHELL_PID=$BSHELL_PID
#echo PPID=$PPID
#echo GRANDPARENT=$GRANDPARENT
#echo SH_PID=$SH_PID
#echo SH_KIDS=$SH_KIDS
#echo ORA8_PID=$ORA8_PID
#echo ORA8_KIDS=$ORA8_KIDS
rm /tmp/$$.*.dat

# **************************************************************************
# * Let's try to kill it with bshcmd6.2
# **************************************************************************
echo '****************************************************************'
echo Sending message to the bshell
bshcmd6.2 -e -u10 -w10 $BSHELL_PID
echo '****************************************************************'
echo Waiting 15 seconds to see if it dies
sleep 15

# **************************************************************************
# * Let's check if the bshell is still around
# **************************************************************************
if [ -z "$(ps -f -o args -p ${BSHELL_PID}| grep bshell6\.2)" ] ;then
echo '****************************************************************'
echo "The session has been successfully terminated"
else
# **************************************************************************
# * Now for the nasty stuff, lets do some kills
# **************************************************************************
echo '****************************************************************'
echo "The session did not terminate... Will now use kill -15"
kill -15 $BSHELL_PID
echo '****************************************************************'
echo Waiting 5 seconds to see if it dies
sleep 5

if [ -z "$(ps -f -o args -p ${BSHELL_PID}| grep bshell6\.2)" ] ;then
echo '****************************************************************'
echo "The session has been successfully terminated"
else
echo '****************************************************************'
echo "The session did not terminate... Will now use kill -9"
kill -9 $BSHELL_PID
echo '****************************************************************'
echo Waiting 5 seconds to see if it dies
sleep 5
fi
fi

echo '****************************************************************'
echo "Making sure that all relevant processes also terminated"
for i in $PPID $GRANDPARENT $SH_PID $SH_KIDS $ORA8_PID $ORA8_KIDS
do
if [ -n "$(ps -f -p $i|grep -v PID)" ] ;then
kill -15 $i
sleep 2
if [ -n "$(ps -f -p $i|grep -v PID)" ] ;then
kill -9 $i
fi
fi
done