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.

if [ -z "$port" ];then
echo "usage: $0 port" >&2
exit 1
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
ps -efw |grep "$ps" |grep -v grep
exit 0

And the sample usage:

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

Return to home | Generated on 09/29/22