My linux has many ports opened by different java processes, most of them are hadoop services.

How can I know which process the port is opened by? I just use the shell script below.

 #!/bin/bash
port=$1
if [ -z "$port" ];then
echo "usage: $0 port" >&2
exit 1
fi
ps=$(lsof -i :"$port" |awk '/LISTEN/ {print $2}')
if [ -z "$ps" ];then
echo "no this port, or I don't have privilege to list the port" >&2
exit 1
fi
ps -efw |grep "$ps" |grep -v grep
exit 0

And the sample usage:

 $ lsport.sh 9000
xxx 1295 1 0 Mar09 ? 01:11:35 /usr/bin/java -Dproc_namenode ...

Return to home | Generated on 09/29/22