1

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?

3 Answers 3

0
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.

0

This one will be a bit more terse; it checks which are the pids of processes named ".*activemq.*" and after that checks their opened ports:

netstat -lp | grep  $(ps aux | awk '/[a]ctivemq/ {print $2}')

You can add a uniq filter if you need to:

netstat -lp | grep  $(ps aux | awk '/[a]ctivemq/ {print $2}' | sort -u)

EDITED BASED ON JOE's COMMENTS:

Joe Nazz wrote:

It doesn't work. The result of """$(ps aux | awk '/[a]ctivemq/ {print $2}' | sort -u)""" are multiple lines, that grep cannot recognize. It comes "No such file or dir..." –

You are right, If there are several processes we need to work a bit more in our grep's regexp expression.

So in order to keep my run-netstat-just-once command, the expression should be something similar to:

~# netstat -lp | grep $(ps aux | awk '/[a]pache/ {a=a"\|"$2} END { sub(/^../,"",a); print "("a")"}')

The command creates a regexp to match each pid of apache (I'm using apache2 as a multi instance process to match your needs). As you'll see in the following expression, the regexp created tries to match every single pid of apache:

~# ps aux | awk '/[a]pache/ {a=a"\|"$2} END { sub(/^../,"",a); print "("a")"}'
(7335\|7336\|7337\|7338\|7339\|8733\|8744\|13418\|13421\|23126)

– Joe Nazz wrote:

[...] But what does the single 'a' in the brackets mean? –

About your question related of why I used the [a] in the regexp, it is a very old trick to avoid matching the process created by the regexp it self. The following example is self explanatory:

~# ps aux | grep foo
root     10932  0.0  0.0   9608   868 pts/0    S+   11:42   0:00 grep foo
~# ps aux | grep "[f]oo"
~# 

PS: if you feel this answer was helpful please don't leave unvoted

3
  • Nice, thx! But what does the single 'a' in the brackets mean?
    – Joe Nazz
    Jul 20, 2011 at 12:17
  • It doesn't work. The result of """$(ps aux | awk '/[a]ctivemq/ {print $2}' | sort -u)""" are multiple lines, that grep cannot recognize. It comes "No such file or dir..."
    – Joe Nazz
    Jul 20, 2011 at 12:25
  • See my post edition. Answers both of your comments
    – hmontoliu
    Jul 23, 2011 at 9:46
0

This retrieves "activemq" pids, tests that one was returned, runs netstat once.

pids=`ps -eo 'pid,args' | awk '/activemq/ && !/awk/ {print $1}'`
test -n "${pids}" && netstat -lp | egrep -w "(`echo ${pids} | tr ' ' '|'`)"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .