Terminate processes for CLOSE_WAIT status of TCP

Hello Friends,

First of all im sorry for spending extra space in DB of forum with this thread, i know there would be a solution if i kept searching,

I need to terminate the process which causes CLOSE_WAIT status of TCP connection via port 8103:

-bash-3.00$ netstat -na | grep 8103
127.0.0.1.8103       127.0.0.1.63876      49152      0 49152      0 CLOSE_WAIT
127.0.0.1.8103       127.0.0.1.39286      49152      0 49152      0 CLOSE_WAIT
127.0.0.1.8103       127.0.0.1.60796      49152      0 49152      0 CLOSE_WAIT
      *.8103               *.*                0      0 49152      0 LISTEN
127.0.0.1.8103       127.0.0.1.62363      49152      0 49152      0 CLOSE_WAIT

I found this script FPMurphy posted on a thread here but i couldnt make it work for Solaris:

PORT=8103
PIDS=`ps -ef | sed 1d | awk '{print $2}'`
for pid in $PIDS
do
        /usr/proc/bin/pfiles $pid 2>/dev/null | grep -q "port: $PORT"
        if [[ $? -eq 0 ]] ; then
               echo "Port: $PORT is being used by PID: \c" 
                           ps -o pid -o args -p $PID | sed 1d
        fi
done

Function of grep -q in Linux is not provided on solaris.. So i got this:

grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .

So i tried below part with xargs even without being sure about how exact output of "/usr/proc/bin/pfiles $pid" part seems like, im sorry a bit stuck with time...

PORT=8103
PIDS=`ps -ef | sed 1d | awk '{print $2}'`
for pid in $PIDS
do
        /usr/proc/bin/pfiles $pid 2>/dev/null | xargs grep "port: $PORT"
        if [[ $? -eq 0 ]] ; then
               echo "Port: $PORT is being used by PID: \c"
                           ps -o pid -o args -p $PID | sed 1d
        fi
done

I had this meaningless output:

grep: can't open 23534:
grep: can't open -bash
grep: can't open Current
grep: can't open rlimit:
grep: can't open 256
grep: can't open file
grep: can't open descriptors
grep: can't open 0:
grep: can't open S_IFCHR
grep: can't open mode:0600
grep: can't open dev:292,0
grep: can't open ino:12582922
grep: can't open uid:100

Any suggestion welcome,

Thanks in advance,
Kind Regards

You can emulate -q with >/dev/null.

You also don't need $? at all.

if /usr/proc/bin/pfiles $pid 2>/dev/null | grep "port: $PORT" >/dev/null
then
       echo "Port: $PORT is being used by PID: \c" 
                   ps -o pid -o args -p $PID | sed 1d
fi

I have had this warning now:

PORT=8103
PIDS=`ps -ef | sed 1d | awk '{print $2}'`
for pid in $PIDS
do
if /usr/proc/bin/pfiles $pid 2>/dev/null | grep "port: $PORT" >/dev/null
then
       echo "Port: $PORT is being used by PID: \c"
                   ps -o pid -o args -p $PIDS | sed 1d
fi
done

root@dx311 # ./port_PID_relation.sh

Port: 8103 is being used by PID: \c
usage: ps [ -aAdeflcjLPyZ ] [ -o format ] [ -t termlist ]
        [ -u userlist ] [ -U userlist ] [ -G grouplist ]
        [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ]
  'format' is one or more of:
        user ruser group rgroup uid ruid gid rgid pid ppid pgid sid taskid ctid
        pri opri pcpu pmem vsz rss osz nice class time etime stime zone zoneid
        f s c lwp nlwp psr tty addr wchan fname comm args projid project pset

ok now i have changed script a bit "pid" instead of $PIDS then i got the output below but still there is a problem PID: \c prints..

PORT=8103
PIDS=`ps -ef | sed 1d | awk '{print $2}'`
for pid in $PIDS
do
if /usr/proc/bin/pfiles $pid 2>/dev/null | grep "port: $PORT" >/dev/null
then
       echo "Port: $PORT is being used by PID: \c"
                   ps -o pid -o args -p $pid | sed 1d
fi
done;
root@dx311 # ./port_PID_relation.sh 
Port: 8103 is being used by PID: \c
28219 /usr/java/bin/java -server -Xms1024M -Xmx1024M -Xloggc:data/log/gc.out -Dhttp.p
Port: 8103 is being used by PID: \c
20613 grep port: 8103

Try this instead of that line:

printf "%s is being used by %s\n" $PORT $(ps -o pid -o args -p $pid | sed 1d)