Finding the process id of the process owning a port

Occasionally there’s time where you need to find out what process owns a port currently open.

On the Mac this can be done easily by using the following line – here we are looking for port 8080:

ps u –no-heading -p `netstat -nlept | awk ‘/[:]8080 / {split ($9,t,”/”); print t[1]}’` 2>/dev/null

For windows you don’t have a decent shell (and cygwin would probably not work here), so you can use the following batch script to do the same:

@echo off

for /F “usebackq tokens=5″ %%f in (`netstat -b -n ^| find “:%1″`) do call :process %%f
goto :eof

tasklist /FI “PID eq %1″ /NH

If the above code was called findport.bat then running findport 8080 would then find the process owning port 8080.

%d bloggers like this: