whatport is a small POSIX shell script that shows which processes are listening on a port. It can also send a signal to those processes, then verify whether the processes exited and whether the port is still in use.
- A POSIX-compatible shell such as
/bin/sh - Either
lsoforss - Standard Unix utilities used by the script:
pskillsedsortwcsleep
Notes:
lsofis preferred when available because it provides clearer process details.- On some systems, seeing full process information may require
sudo. - The script checks TCP listeners in
LISTENstate and also shows UDP sockets bound to the port.
Make the script executable:
chmod +x whatportRun it from this directory:
./whatport 3000If you want to use it from anywhere, move it into a directory on your PATH, for example:
cp whatport /usr/local/bin/whatport
chmod +x /usr/local/bin/whatportwhatport PORT
whatport -k [-y] [-s SIGNAL] PORTExamples:
whatport 3000
whatport --kill 3000
whatport -k -y 3000
whatport -k -y -s KILL 3000-
-k,--killSend a signal to processes listening on the port. -
-y,--yesSkip the confirmation prompt before killing. -
-s,--signal SIGNALSignal to send when killing. Default isTERM. -
-h,--helpShow built-in help text.
When you run:
whatport 3000the script:
- Validates that
3000is a valid port number. - Finds processes listening on that port.
- Prints process details.
When you run:
whatport -k 3000the script:
- Shows the current listeners.
- Prompts for confirmation unless
-yis used. - Sends the requested signal to the matching PID or PIDs.
- Checks whether those PIDs are still running.
- Checks whether the port is still in use.
It exits successfully only when the kill path completes and the original PIDs are gone and the port is no longer in use.
0on successful lookup when listeners are found0on successful kill and verification1when no listeners are found, arguments are invalid, required tools are missing, or the kill/verification step fails
Show what is listening on port 8080:
./whatport 8080Kill the listener on port 8080 and confirm interactively:
./whatport -k 8080Force kill without prompting:
./whatport -k -y -s KILL 8080- If you get no process details, try running with
sudo. - If the script says no listeners were found, verify the service is actually bound to that port.
- If the port closes but the process remains alive, the script treats that as a failed kill verification.