Bash – Howto check open ports of a specific tomcat java instance

activemqbashjavashelltomcat

For the moment I've managed to do it with:

root@server:~# for i in $(netstat -lp | grep java | awk '{print $7}' | awk -F '/' '{print $1}' | sort | uniq); do ap=$(ps p $i | grep -v PID | grep activemq | awk '{print $1}'); done; netstat -lp | grep $ap
tcp        0      0 localhost:32000         *:*                     LISTEN      23059/java      
tcp6       0      0 HPM.DMZ:61616           [::]:*                  LISTEN      23059/java      
tcp6       0      0 [::]:8161               [::]:*                  LISTEN      23059/java      
tcp6       0      0 [::]:36168              [::]:*                  LISTEN      23059/java

It checks all process IDs belonging to open network ports, checks whether or not it is a tomcat instance "activemq".

Any better suggestions?

Best Answer

for i in $(ps aux | awk '/activemq/ {print $2}' | sort -gu); do netstat -lp | grep $i; done

tcp        0      0 localhost:32000         *:*                     LISTEN      23059/java      
tcp6       0      0 HPM.DMZ:61616           [::]:*                  LISTEN      23059/java      
tcp6       0      0 [::]:8161               [::]:*                  LISTEN      23059/java      
tcp6       0      0 [::]:36168              [::]:*                  LISTEN      23059/java

was shorter but it calls netstat multiple times.

Related Topic