1 Reply Latest reply: Aug 18, 2013 8:18 AM by 800808 RSS

    Not able to find PID responsible for hogging a port socket

    800808

      Dear Solaris Gurus,

       

      I found the following script from https://blogs.oracle.com/JoachimAndres/entry/solaris_which_process_is_bound1 below which only partly work by not locating the PID stil:

       

      [code]1. #!/bin/ksh

      2. line='---------------------------------------------'
      3. pids=$(/usr/bin/ps -ef -o pid=)

      4. if [ $# -eq 0 ]; then
      5.     read ans?"Enter port you would like to know pid for: "
      6. else
      7.     ans=$1
      8. fi

       

      9.  for f in $pids
      10. do
      11.     /usr/proc/bin/pfiles $f 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
      12.     if [ $? -eq 0 ]; then
      13.         echo
      14.         $line
      15.         echo "Port: $ans is being used by PID:\\c"

      16.         pargs -l $f
      17.         #/usr/bin/ps -o pid,args -p $f
      18.     fi
      19. done
      20. exit 0[/code]

       

      Below is the output from running the above script:

       

      [code]root@hotpotato # ./findPIDLockTomcatPort.ksh


      Enter port you would like to know pid for: 17005

      ---------------------------------------------

      Port: 17005 is being used by PID:/export/home/support/jdk1.6.0_10/bin/java '-Djava.util.logging.config.file=/export/home/support/tomcatProd/conf/logging.properties' -Xms768m -Xmx768m '-XX:MaxPermSize=256m' '-Dqubit.env=prod' '-Dxr.load.xml-reader=org.ccil.cowan.tagsoup.Parser' '-Djava.awt.headless=true' '-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager' '-Djava.endorsed.dirs=/export/home/support/tomcatProd/endorsed' -classpath :/export/home/support/tomcatProd/bin/bootstrap.jar '-Dcatalina.base=/export/home/support/tomcatProd' '-Dcatalina.home=/export/home/support/tomcatProd' '-Djava.io.tmpdir=/export/home/support/tomcatProd/temp' org.apache.catalina.startup.Bootstrap start

       

      Looks like the bug is from line 15 especially \\c but I dont know under why it is there for. I interpret \\c as escape of carriage return character. Could someone assist with identifying where the problem is? [/code]

       

      Many thanks in advance,

       

      George

        • 1. Re: Not able to find PID responsible for hogging a port socket
          800808

          Dear All,

           

          I managed to identify & correct the error by replacing the for with a while loop and directed the list of all process ID to a file instead of variable pids as follows:

           

          [code]#!/bin/ksh
          WORKING_DIR=/tmp; export WORKING_DIR
          LIST_OF_RUNNING_PROCESSES_IN_DESCENDING_ORDER=${WORKING_DIR}/list_of_running_processes_in_descending_order.txt; export LIST_OF_RUNNING_PROCESSES_IN_DESCENDING_ORDER

          rm -f ${LIST_OF_RUNNING_PROCESSES_IN_DESCENDING_ORDER}

          /usr/bin/ps -ef | sed 1d | awk '{print $2}' | awk '{a[i++]=$0}END{for(j=i-1;j>=0;j--)print a[j];}' > ${LIST_OF_RUNNING_PROCESSES_IN_DESCENDING_ORDER}

          if [ $# -eq 0 ]; then
          read ans?"Enter port you would like to know pid for: "
          else
            ans=$1
          fi

          while read CURRENT_PROCESS_ID
          do
          /usr/proc/bin/pfiles ${CURRENT_PROCESS_ID} 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
          if [ $? -eq 0 ]; then
            echo "Port: $ans is being used by Process ID: ${CURRENT_PROCESS_ID}"
            break
          fi
          done < ${LIST_OF_RUNNING_PROCESSES_IN_DESCENDING_ORDER}[/code]

           

          However, I am would like to know why the for loop didn't work. Also, if someone could explain how pfiles work would be appreciated.

           

          Thanks,

           

          George