There is two ways to find PID by knowing which port is used:
P.S. All information was get over the web just as reminder for myself.
Lsof (LiSt Open Files)
lsof | grep <port_number>
1. lsof doesn’t work from non-global zone.
2. Use -z option with lsof to list open files & processes with in non-global zone like “lsof -z”
2. Use -z option with lsof to list open files & processes with in non-global zone like “lsof -z”
Script for know the process for port using
#!/usr/bin/ksh
#
#grep -v "^#" /etc/services | awk '{print $2}' | cut -d"/" -f1 | sort -n | uniq >sorted-services
line='-------------------------------------------------------------------------'
pids=`/usr/bin/ps -ef | sed 1d | awk '{print $2}'`
# Prompt users or use 1st cmdline argument
while [ $# -eq 0 ]; do
if [ $# -eq 0 ]; then
read ans?"Enter port you like to know pid for: "
else
ans=$1
fi
# Check all pids for this port, then list that process
for f in $pids
do
/usr/proc/bin/pfiles $f 2>/dev/null | /usr/xpg4/bin/grep -q "port: $ans"
#/export/baha/sorted-services $f 2>/dev/null | /usr/xpg4/bin/grep -q "$ans"
if [ $? -eq 0 ] ; then
echo "$line\nPort: $ans is being used by PID: \c"
/usr/bin/ps -o pid -o args -p $f | sed 1d
fi
done
done
#if [ $# -eq 0 ]; then
# read ans?"Enter another port you like to know pid for: "
#else
# ans=$1
#fi
#exit 0
P.S. All information was get over the web just as reminder for myself.
Comments
Post a Comment