J3st3r J3st3r - 10 months ago 52
Bash Question

parsing data from ps -ef

I have my output below:

ps -ef | grep logstash
kafka 2832 1 1 Oct06 ? 08:18:36 /usr/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/apps/COMPASSLOG_DEV/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/apps/COMPASSLOG_DEV/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/apps/COMPASSLOG_DEV/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/apps/COMPASSLOG_DEV/logstash-2.2.2/vendor/jruby -Djruby.lib=/apps/COMPASSLOG_DEV/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /apps/COMPASSLOG_DEV/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /apps/COMPASSLOG_DEV/COMPASSLOG_DEV.config

I have used the script below to make it look clean:

ps -ef | grep logstash | grep -v grep | grep -v nohup|awk '{print $38}'|awk -F/ '{print $3}'|awk -F_ '{print "Logstash is running for: "$1$2}'

which gives me output:

Logstash is running for: COMPASSLOGDEV

How can I add the port number its running on as below?

Logstash is running for: COMPASSLOGDEV on port number 2832
^^^^^^^^^^^^^^^^^^^ add this

Answer Source
ps -ef | grep logstash | awk -F" " '{print "Logstash is running for " substr($NF,7,10) substr($NF, 18,3)" on port number " $2 }' 

Using space as delimiter:

$2 is port 2832

substr($NF,7, 10) is COMPASSSLOG

substr($NF, 18, 3 is DEV

The above get what you want, but as @Farhad mentioned. 2832 IS PID OTHER THAN PORT NUMBER