Wednesday, April 21, 2010

Linux Shell script to find the process listening on a given port

procinfo=$(netstat --numeric-ports -nlp 2> /dev/null grep ^tcp grep -w ${port} tail -n 1 awk '{print $7}')
case "${procinfo}" in
echo "No process listening on port ${port}"
echo "Process is running on ${port}, but current user does not have rights to see process information."
echo "${procinfo} is running on port ${port}"
ps -uwep ${procinfo%%/*}