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 Masroof Ahmad

Leave a Comment:

13 comments
Add Your Reply