Jump to content

Logo

- - - - -

Solved : Script to find which process is using a port number on Solaris


1 reply to this topic

#1 men

    Hero Member

  • Root Admin
  • PipPipPipPip
  • 4,389 posts
  • Gender:Male
  • Location:Europe

Posted 27 October 2009 - 03:01 PM

Hello,

This can be useful on Solaris when lsof is not available:


bash-2.05# cat find_PID_from_port
#!/bin/ksh
#*******************************************************************************
**************************#
# Purpose is to find the PID on which given port is active.
# Need to be root to check other folk's PID's out
#
# Note: This script gives the PID and process details which is using the port number given in argument
# ./find_PID_from_port 80
#*******************************************************************************
**************************#

line='-------------------------------------------------------------------------'
pids=$(/usr/bin/ps -ef | sed 1d | awk '{print $2}')

# Prompt users or use 1st cmdline argument
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"
		if [ $? -eq 0 ]; then
				echo $line
				echo "Port: $ans is being used by PID:\c"
				/usr/bin/ps -ef -o pid -o args | egrep -v "grep|pfiles" | grep $f
		fi
done
exit 0

To be found with an output example at http://unixadministrator.blogspot.com/2009...ort-number.html (please note I had problems viewing the page with IE, not with Firefox nor Chrome).

Michel

#2 MKE

    Junior Member

  • Members
  • Pip
  • 29 posts
  • Gender:Male

Posted 30 June 2010 - 01:58 PM

Thanks Michel, this will be really useful for $Universe shutdowns. ;-)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users