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