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.

Author: petermount1

Prolific Open Source developer who also works in the online gaming industry during the day. Develops in Java, Go, Python & any other language as necessary. Still develops on retro machines like BBC Micro & Amiga A1200

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: