Determine the process pid listening on a certain port
The -p
flag of netstat
gives you PID of the process:
netstat -l -p
Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat
.As we worked out during the discussion with @Cyclone, the line that does the job is:
sockstat -4 -l | grep :80 | awk '{print $3}' | head -1
netstat -p -l | grep $PORT
and lsof -i :$PORT
solutions are good but I prefer fuser $PORT/tcp
extension syntax to POSIX (which work for coreutils
) as with pipe:
pid=`fuser $PORT/tcp`
it prints pure pid so you can drop sed
magic out.
One thing that makes fuser
my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):
$ fuser -k $port/tcp # with SIGKILL$ fuser -k -15 $port/tcp # with SIGTERM$ fuser -k -TERM $port/tcp # with SIGTERM
Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser