Getting correct port number from process id

Hi All,

i am trying to find the Jobss port number(either default port number or any other port number assigned) from the running process id.

But it's giving me multiple port numbers when searching with netstat command. Can someone help me in finding the correct port number from the process id.

Below is the process id for Jboss :

# ps -ef |grep jboss
root      2045  2001 18 15:49 pts/0    00:00:07 /opt/jdk1.8.0_161/bin/java -D[Standalone] -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.boot.log.file=/opt/wildfly/standalone/log/server.log -Dlogging.configuration=file:/opt/wildfly/standalone/configuration/logging.properties -jar /opt/wildfly/jboss-modules.jar -mp /opt/wildfly/modules org.jboss.as.standalone -Djboss.home.dir=/opt/wildfly -Djboss.server.base.dir=/opt/wildfly/standalone
# netstat -anlp |grep 2045
tcp        0      0 0.0.0.0:9990            0.0.0.0:*               LISTEN      2045/java
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2045/java
unix  2      [ ]         STREAM     CONNECTED     43013    2045/java
unix  2      [ ]         STREAM     CONNECTED     43029    2045/java

can someone help in finding the correct port number from the process id.

Not clear what exactly you want. How far would this get you?

netstat -anlp | awk -v"PID=$(pgrep jboss)" '$NF !~ PID {next} $(NF-1) == "LISTEN" {n=split ($4, T, "[.:]"); print T[n]} $(NF-2) == "CONNECTED" {print $(NF-1)}'
9990
8080
43013
43029

Hi Rudi,

i need to find exact/correct port number from the process id.

Both are exact ports.

Currenty jboss is running in standalone mode, with 9990 being management port (jboss cli and HTTP access for deploying restarting etc...)
And 8080 being application served.
In future you can configure it to serve additional JVM with application on port 8180, for instance, then 3 ports would be 'exact' so to say.

Also, on systems configured with IPv6, you might get even more additional exact ports :slight_smile:

Perhaps using lsof ?

lsof -aPp $(pgrep jboss) -iTCP -sTCP:LISTEN -i4

Hope that helps
Regards
Peasant.