Find which process listens to a port

Sometimes I need to find out which process listens to a port to kill it.

On macOS,

sudo lsof -i -P | grep LISTEN | grep :$PORT

and on Linux

netstat -pntl | grep $PORT