Get opened port with given PID?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get opened port with given PID?
# 1  
Old 07-20-2011
Get opened port with given PID?

i want to get tomcat listening port , from a command.

Code:
ps -ef | grep catalina | grep -v "grep catalina" | grep -v "catalina.out" | awk '{print $2}' | head -1

output : 
-----
1234

Now with this 1234 i need to know , in which port my tomcat is running...

i tried ,
Code:
netstat -ao | grep "1234"
lsof -p 1234


Code:
fuser 8080/tcp

will give the exact output with pid.


But i need pid as input and port as output

is there any other way ?
# 2  
Old 07-20-2011
What do you mean by
Quote:
But i need pid as input and port as output
??

Do you want to write an interactive script with some read commands or... ? What do you mean by input and output? For which tool/script is this input/output?
Please explain what you want to achieve, thanks.
# 3  
Old 07-20-2011
i am sorry if its my mistake.

My need is to get tomcat listening port through a shell script.
# 4  
Old 07-20-2011
You can it do the way you already did or check the server.xml of your Tomcat where the ports are defined in and grep it out of there. When having the port(s), you can just check additionally with
Code:
netstat -an| grep <port>

if it is up and running.
The way you have chosen, you do not know how many Tomcat installations/configurations are there, but just check which is up and try to get it's port.
It depends which way you want to go.
# 5  
Old 07-20-2011
Quote:
Originally Posted by linuxadmin
But i need pid as input and port as output

is there any other way ?
You should be able to grep the port tomcat is listening on from the output of lsof
# 6  
Old 07-21-2011
Quote:
Originally Posted by shamrock
You should be able to grep the port tomcat is listening on from the output of lsof
Dear shamrock, i tried as you told
Code:
[root@linux ~]# lsof | grep 8080
automount  2867      root  mem       REG       8,11      98080    1006186 /usr/lib/autofs/lookup_file.so

And zaxxon, what you told is correct only. I can get it from server.xml file.
But my problem is : we have to use this script in a number of servers. And the server.xml file may be manually edited. There will be http & https connections also. The part <Connector port="portNum" will edit manually. And there is a chance to change the alignment also.
Code:
[root@linux ~]# grep 8080 /tomcat6/apache-tomcat-6.0.18/conf/server.xml
         Define a non-SSL HTTP/1.1 Connector on port 8080
    <Connector port="8080" protocol="HTTP/1.1" 
               port="8080" protocol="HTTP/1.1"

So upto my knowledge , there is only one way , that to GET the PORT number from PROCESS id.
Code:
[root@linux ~]# ps -ef | grep tomcat
root      7976     1  0 Jul18 ?        00:22:11 /usr/java/jdk1.6.0_13/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/tomcat6/apache-tomcat-6.0.18/conf/logging.properties -verbose:gc -XX:+PrintGCDetails -XX:MaxPermSize=512M -Xms1024M -Xmx1024M -Djava.awt.headless=true -XX:+UseParallelOldGC -XX:+UseParallelGC -Djava.endorsed.dirs=/tomcat6/apache-tomcat-6.0.18/endorsed -classpath :/tomcat6/apache-tomcat-6.0.18/bin/bootstrap.jar -Dcatalina.base=/tomcat6/apache-tomcat-6.0.18 -Dcatalina.home=/tomcat6/apache-tomcat-6.0.18 -Djava.io.tmpdir=/tomcat6/apache-tomcat-6.0.18/temp org.apache.catalina.startup.Bootstrap start
root     28214 28183  0 10:02 pts/1    00:00:00 grep tomcat

[root@linux ~]# ps -ef | grep catalina | grep -v "grep catalina" |  grep -v "catalina.out"|awk '{print $2}' | head -1
7976

[root@linux ~]# fuser 8080/tcp
8080/tcp:             7976       -->> I need vice-versa of this. That is , by giving process id , i should get port number. I tried by grep of fuser but i couldn't find.

# 7  
Old 07-21-2011
Shamrock's idea is correct and good. The only difference is, that if you want an absolute list of ports of configured ports (not only those that are currently up and running), you will not get it by checking which processes are there, since it could be, that one of them is down for maintenance or similar.
For the problem of the format the server.xml could be in due to manual editing, I would try to run following via ssh from one box that has hopefully access to all others so you don't have to log in into each of them. For stuff like this I usually do something like the following (of course depending they are in a similar directory - else you have to think about how to find those server.xml in different locations):

Code:
$> cat server.list
server_1
server_2
server_3
$> while read S; do ssh -n -o "ConnectTimeout=3" -o "BatchMode yes" $S "cat ~tomcat/conf/server.xml" | sed -n 's/.*Connector port="\([^"]*\)" .*/\1/p'

On a box where 3 instances of Tomcat are running, I get something like:
Code:
8080
8443
8444
8445
8446
8447
8009

Which are up and running you could get the other way with ps and lsof.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

How to find port number wwn of particular port on dual port HBA,?

please find the below o/p for your reference bash-3.00# fcinfo hba-port HBA Port WWN: 21000024ff295a34 OS Device Name: /dev/cfg/c2 Manufacturer: QLogic Corp. Model: 375-3356-02 Firmware Version: 05.03.02 FCode/BIOS Version: BIOS: 2.02; fcode: 2.01;... (3 Replies)
Discussion started by: sb200
3 Replies

2. UNIX for Advanced & Expert Users

Find PID using a Port?

Hi, I do not have root user credentials nor do I have the functional id of the process that uses port 80. How can I find the pid of the process using the port number 80 ? Operating System: Linux (6 Replies)
Discussion started by: mohtashims
6 Replies

3. HP-UX

Find port for Pid

Hi, Is this the most appropriate way of finding the listen port number given the pid is "16659" ? lsof -Pan -i tcp -i udp | grep 16659 | grep -i "listen"If so, how can I extract "7001" and assign it to a variable say myport=7001 from the below output which happens to be actual port number? ... (1 Reply)
Discussion started by: mohtashims
1 Replies

4. UNIX for Dummies Questions & Answers

Find PID for a port

Hi, I need to find the PID for a given port on the below system. HP-UX mymachine B.11.31 U ia64 3223107173 unlimited-user license How can I ? (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

6. UNIX for Dummies Questions & Answers

pid from port number in AIX

Hello guys, How to shut down a port number in AIX. May be first I need to find out what is the process ID of that process that listens to this particular port.. Is there any command to find a process ID from the port number other than "lsof". thanks (1 Reply)
Discussion started by: solaix14
1 Replies

7. HP-UX

To find pid from port number

Hi, I am working on HP-UX Release 11i. I want to find the process id (PID) of the process running on a particular port. lsof command fuser does not work on this system. Please suggest some alternative. Thanks (6 Replies)
Discussion started by: gmat
6 Replies

8. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies
Login or Register to Ask a Question