Shailendra - I do not know if it is your script or just that you only pasted a partial script but this will always loop 6 times and then issue a kill -9 (after prompting)
Doing a kill -9 is not advisable. The initial way without the -9 is much cleaner (it looks like your script may do this somewhere else?). It will take time to shut your server down but it also allows it to clean up the DMS/JMS/DAS_GSA_SUBSCRIBER (among other) queues and tables. This will prevent issues as you stop and start servers.
kill -9 is great IF the server is a state that it is totally hung and not recovering or shutting down.
Our stop scripts typically issue the kill <pid>, wait for a period of time (2-5 minutes) and then, if the process is still running, reissues the kill with the -9 option.
KdM :
What you are doing is not wrong but more complicated then it needs to be. Sending a kill to the pid (in your example 20169) should shut down the JBoss instance cleanly with no issues (assuming that 20169 is your java process). Is this locally in a development env or a managed/production environment?
The pstree and sed commands below are unnecesary as near as I can tellcd
There are a dozen ways to pull the PID out (the awk Shailendra has below is fine). Pass that to the following :
function stopInstance {
PID=$1
#sending a QUIT to do a thread dump to console
kill -QUIT $PID
#sending a interrupt which should stop jboss
kill $PID
# give the instance 2 minutes to shut down, and then kill it if it hasn't
waitTime=0
until [ $waitTime -gt 120 ]; do
sleep 2
ps -p $PID>/dev/null
if [ $? != 0 ]; then
break
fi
let waitTime+=2
done
ps -p $PID>/dev/null
if [ $? = 0 ]; then
kill -9 $PID
fi
}