Running and experimenting with Node.js apps, I often play around and forgot I started the app already or it’s already running on the port I wanted to start it up on now.

1. What’s the PID so I can kill it 🥊?

lsof is a powerful command utility for this, it stands for “list open files”. Despite the name, it can list much more than just files - it shows all open file descriptors, including network connections, which is exactly what we need for port troubleshooting.

Basic usage:

lsof -i :port

Using lsof with -i to list network connections and :port to look at a specific port (like 5000, 8000, 3000, etc.), I can find the PID and use kill -9 [pid] to force terminate the application I no longer want running on that port.

Example workflow:

# Check what's running on port 3000
lsof -i :3000

# Output might look like:
# COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
# node     1234   user    20u  IPv4  0x123    0t0  TCP *:3000 (LISTEN)

# Kill the process
kill -9 1234

Understanding the kill command:

  • kill -9 sends a SIGKILL signal to the process
  • Unlike kill (SIGTERM), SIGKILL cannot be ignored by the process
  • With kill -9 you’re not asking the application to terminate gracefully - you’re telling the OS to immediately stop running the program regardless of what the application is doing
  • Use with caution - the process won’t have a chance to clean up resources, close files, or save data

Better approaches:

# Try graceful termination first (SIGTERM)
kill [pid]

# If that doesn't work after a few seconds, then force kill
kill -9 [pid]

# Alternative: Find and kill by process name
pkill -f "node.*3000"  # Kill node processes with 3000 in command

# On some systems, you might prefer:
killall node  # Kills all node processes (be careful!)

Other useful lsof examples:

# List all network connections
lsof -i

# List connections on specific interface  
lsof -i tcp:80

# Show what files a specific process has open
lsof -p [pid]

# Show what processes are using a specific file
lsof /path/to/file

Resources: