How to find PID listening particular Port on Linux/Solaris 10

If you are trying to run any apps service (adapcctl.sh, opmnctl, lsnrctl) and get message like  “unable to bind port <port_number>” (Most probably this port is in use by other process), fix is to either start your service on different port or terminate process listening on that particular port using kill -9 <pid>

In order to kill that specific pid, you need to find pid which is listening on that specific port. There are lot of ways to find pid which is listening/using specific port and most commonly used is LSOF

What is lsof ?
lsof which stands for LiSt Open Files is Unix utility to list all open files and processes that opened them on Unix.


How to use LSOF ?

lsof | grep <port_number>  ( on Solaris,lsof is usually installed under /usr/local/bin where as on Linux its usually under /usr/sbin )

Beware if you are on Solaris 10 and using ZONES(more on zones in solaris 10 coming soon) :
1. lsof doesn’t work from non-global zone in solaris 10
2. Use -z option with lsof to list open files & processes with in non-global zone like “lsof -z
3. Solaris 10 with zones list doesn’t display port , hence I used script mentioned at end of this post.

Once you know PID using specific port, you can either stop it or terminate using
kill -9 <pid>

Key points for lsof on solaris

1) Installation procedure of lsof on solaris is same for Solaris Sparc or X86 & on version 9 or 10 only difference is file name

2) lsof doesn’t work from zone  (More on zones in solaris 10 coming soon), You have to install lsof on global zone.  If you run lsof and get error message like “lsof: can’t stat(/devices): No such file or directory” probably you are running lsof with in solaris 10 Zone.

3) lsof on solaris is installed under /usr/local/bin (default location)

4) On Solaris 10, using “lsof -i” to show mapping of processes to TCP ports incorrectly shows all processes that have socket open as using port 65535. (Use script mentioned at end of file in such cases)

How to install lsof on Solaris 10
lsof is part of Red Hat Linux (By default its installed under /usr/sbin), for Solaris lsof is available as freeware here for Solaris Sparc 10 download it  from here

1. Download lsof executable for Solaris 10 on sparc from here  or from here if you are on different processor or version.

2. Upload zip file like lsof_0606-4.77-sol10-sparc-local.gz  to server

3. Unzip as gunzip lsof_0606-4.77-sol10-sparc-local.gz  (This will create file like lsof_0606-4.77-sol10-sparc-local)

4. pkgadd -d lsof_0606-4.77-sol10-sparc-local  (Run this command from root user from global zone)

If you are on Solaris 10 use below script to list port

create file like get_pid_from_port.sh   (Or download script from here)

#!/bin/bash
# $1 is the port we are looking for

if [ $# -lt 1 ] then
echo “Please provide a port number parameter for this script”
echo “e.g. $0 1521″
exit
fi

echo “Greping for your port, please be patient (CTRL+C breaks) … “

for i in `ls /proc`
do
pfiles $i | grep AF_INET | grep $ 1
if [ $? -eq 0 ] then
echo Is owned by pid $i
echo ——
fi
done

and execute this script like
sh get_pid_from_port.sh <port_num>
sh get_pid_from_port.sh 8000 (To find pid using port 8000)

(I got this script from net last year and now I can’t find Author of this script. In case I find authour or link I’ll remove from here and link to original site)

Related

LSOF FAQ
Quick Start for LSOF 

About the Author Atul Kumar

Oracle ACE, Author, Speaker and Founder of K21 Technologies & K21 Academy : Oracle Gold Partner specialising in Design, Implement, and Trainings.

follow me on:

Leave a Comment:

12 comments
unknown says June 22, 2008

This command is not limited till linux/Solaris. You can use lsof in AIX also.

Syntax

lsof -f TCP:port

The output will give you the PID

Reply
Anon says June 28, 2008

We can use,

# netstat -ap | grep

Reply
Anandan says February 17, 2009

Thanks Atul Kumar

Reply
senthilk says March 11, 2009

hi,
use the command in linux
lsof -i:portnumber

Reply
iza_capo says July 8, 2009

hi;
for your step 4 above
>>4. pkgadd -d lsof_0606-4.77-sol10-sparc-local

can you create a command for noninteractive one?
thanks

Reply
anonymos says November 28, 2009

One small correction:

The line:

“pfiles $i | grep AF_INET | grep ”

should be:

“pfiles $i | grep AF_INET | grep $1″

Reply
Atul Kumar says December 1, 2009

Thanks Anon

Reply
Félix says December 10, 2009

Thanks Atul, the shell work so fine.

Reply
jurgyman says June 16, 2010

WARNING:
use of pfiles on a process PAUSES the process.
this burned me on a real-time critical app server where I was using pfiles to count in and outbound connections.

it sux there is no native solaris support for: netstat -p or lsof -i like functionality.

Reply
Ashish says January 21, 2013

Hi Atul,

I have to grep processes sometimes in following manner to kill them :

/usr/ucb/ps -awwx|grep java|grep

and then
kill -9

I have around 10 processes for which i have to execute above commands sequentially to restart some services on my server.

Please can you advise me the way so that i can grep all process ids at the same time with a single command and then kill them. Please advise if a script can be made which will grep all the process id i required and return them as a output.

I am waiting for your reply :)

Thanks,
Ashish

Reply
Finder App For Linux | Free Documents App says January 19, 2015

[…] » How to find PID listening particular Port on Linux … – This command is not limited till linux/Solaris. You can use lsof in AIX also. Syntax. lsof -f TCP:port. The output will give you the PID… […]

Reply
Add Your Reply