Working with ports

almirsarajcic

almirsarajcic

Created 4 months ago

Sometimes you need to find out which ports you have open, find a process listening on a specific port, or even kill the process. Here’s how.

# Find all open ports
lsof -i 4 -a | grep LISTEN

# Find the process that's listening on a specific port
lsof -i :5432

# Find the process and kill it
lsof -i :5432 | awk '{print($2)}' | xargs -I '{}' kill -9 {}

We’ve used this in Backing up Fly.io PostgreSQL DB with a one-liner.