Function call is not happening

set -x
QM=$1
port=$2
 
KILLQ ()
{
ps -efww | grep  $QM | grep -v grep | tr -s "  " " " | cut -d" " -f2 |xargs kill -9
sleep 3
echo "Checking the Queue"
CHKQ
}
 
 
CRTQ ()
{
strmqm $QM
runmqlsr -m $QM -t TCP -p $port &
}
 
CHKQ ()
{
count=`netstat -anp | grep $port |wc -l`
if [ $count -eq 0 ]
then
echo $count
echo "Creating the Queue"
CRTQ
sleep 3
else
echo $count
echo "Killing the Queue"
KILLQ
sleep 3
fi
}
 
echo "Checking the Queue"
CHKQ

[/CODE]

Above function call CHKQ is not getting called for the 2nd time.

in debug mode

Checking the Queue
+ CHKQ
++ wc -l
++ grep 11460
++ netstat -anp
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
+ count=11
+ '[' 11 -eq 0 ']'
+ echo 11
11
+ echo 'Killing the Queue'
Killing the Queue
+ KILLQ
+ grep -v grep
+ tr -s '  ' ' '
+ cut '-d ' -f2
+ xargs kill -9
+ grep L3LikeQueueManager
+ ps -efww
Killed

[/CODE]

curious that y CHKQ is not called for the 2nd time

Shell functions are never invoked unless they are called "outside" of the function code itself. (I'm ignoring recursion)

The only shell function you call is CHKQ. That code as written should call CHKQ one time only- and CHKQ then invokes two other functions.

but KILLQ does invoke CHKQ, Why is it not getting invoked?

Above is an infinite loop. Is that on purpose?

You kill yourself since L3LikeQueueManager is in your scripts arguments as well. But a while loop would be better then having the two functions call each other and potentially filling up the stack.

Consider the following more precise match

count=`netstat -an | awk '$4~P' P=":$port$"`

and the following more precise kill

pidlist=`ps -eo pid,args | awk '$2~QM {print $1}' QM="$QM"`
if [ -n "$pidlist" ]; then
  kill $pidlist
fi

Or even an ultra-short

pkill -x "$QM"

Rudi - It is not an infinite loop, when the count is 0 It is creating my QManager.
If it was infinite then the script should have never ended.

Scott- I'll give a try with while loop :slight_smile:

MIG - That is pretty complex, But thanks for the help. If i get stuck somewhere i'll ask your help.