Hacker News new | past | comments | ask | show | jobs | submit

Show HN: Sonar – A tiny CLI to see and kill whatever's running on localhost

https://github.com/RasKrebs/sonar
loading story #47458499
Would be nice to have a flag to customize the URL displayed for Docker containers. I connect to my host via Tailscale, but I can’t open links with localhost. It would be helpful to have a parameter that allows us to choose a network device or specify an IP address to display.

    3000    wud (getwud/wud:latest)                            wud          getwud/wud:latest                       3000    http://localhost:3000
    3001    dockhand (fnsys/dockhand:latest)                   dockhand     fnsys/dockhand:latest                   3000    http://localhost:3001
loading story #47454564
loading story #47457270
I always have a bunch of local projects running, particularly during the weekend where I'm rarely working on one thing at a time. A big pain of mine was constantly running into port: Redis from one project blocking another, orphaned dev servers from old worktrees, Docker containers I forgot about. The usual fix is lsof -iTCP | grep ..., then figuring out what the PID actually is, then killing it. But I always forget the command, and it doesn’t really include all the information that I like.

So I built this lightweight CLI. Single binary, no dependencies. It shows everything listening on localhost with process names, Docker container info, clickable URLs etc.

Sure there are workarounds, but none that satisfied my need for a short, easily rememberable command. Also nothing really has the same satisfaction as running sonar kill 3000 — it just feels nice. I’ve already been approached by a few agent orchestration tools that have been struggling with the same thing. It's really useful when you have multiple agents running, but it's not built for just that use case, I have also find it handy when killing off all containers after a failed cleanup and so on. Also know that MCPs are dead and CLIs are the new thing in agentic coding, this might be a useful tool for Claude, particularly when a compose process exits before all containers are stopped.

Open for contributions, ideas and feedback.

loading story #47453797
loading story #47453662
Nice! I always forget the lsof flags and end up googling them every time. Would be cool if it could run in the system tray and show what's running on your ports at a glance. Also, the name had me thinking SonarQube at first, might be worth considering a rename to avoid the confusion.
loading story #47455466
Mine is called “porthole”

https://github.com/clutchski/dotfiles/blob/main/home/bin/por...

loading story #47456062
loading story #47454707
I am absolutely installing this and starting to use it daily!

For the even less patient there's also this (not mine): https://github.com/jkfran/killport

loading story #47454625
I just have an alias in my .bashrc :)
loading story #47456562
loading story #47457846
I always find myself going through my zsh history for `lsof`. Will definitely check this out, seems interesting (even though I'm generally reluctant of installing third-party tools for such jobs).
loading story #47454804
Christ Almighty I hate our industry practice of binding to some inscrutable port number on localhost. Unix domain sockets aren't that hard! They're secure against all sorts of attacks and more convenient to boot. Instead of connecting to a number, you connect to a file. An ordinary file, with an ordinary name you can mv, chmod, and rm. Boring on a good way.

So why doesn't everyone run local services over Unix sockets?

The only problems: 1) web browsers don't support AF_UNIX URI scheme, and 2) ancient versions of Java don't have built-in APIs for AF_UNIX sockets.

That's it. For these trivial reasons, we've beat our head against arbitrary opaque numbers for decades.

And so, for want of a nail, the Unix was lost.

Sonar as in SonarQube? That's an interesting choice for a name :)
loading story #47454456
loading story #47454837
loading story #47457192
love this, i get tired of spamming lsof -i tcp:xxxx
loading story #47453584
loading story #47453246
love this, happens too often
loading story #47454692
loading story #47458624
loading story #47457837
i have this in my .zshrc which provides same functionality:

  lk() {
    if [ $# -eq 0 ]; then
        local output=$(sudo lsof -iTCP -sTCP:LISTEN -n -P)
    elif [ $# -eq 1 ]; then
        local output=$(sudo lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color=always $1)
    else
        echo "find and kill processes listening on ports. Usage: lk [pattern]"
        return 1
    fi

    if [ -z "$output" ]; then
        echo "No listening processes found."
        return 0
    fi

    # Show header + results
    echo "$(sudo lsof -iTCP -sTCP:LISTEN -n -P | head -1)"
    echo "$output"
    echo ""

    # Extract unique PIDs (skip the header row if no grep was applied)
    local pids=($(echo "$output" | awk '{print $2}' | grep -E '^[0-9]+$' | sort -u))

    if [ ${#pids[@]} -eq 0 ]; then
        echo "No PIDs found."
        return 0
    fi

    echo "PIDs to kill: ${pids[*]}"
    echo -n "Kill these ${#pids[@]} process(es)? [y/N] "
    read -r confirm

    if [[ "$confirm" =~ ^[Yy]$ ]]; then
        for pid in "${pids[@]}"; do
            echo "Killing PID $pid..."
            sudo kill -9 $pid
        done
        echo "Done."
    else
        echo "Aborted."
    fi
  }
loading story #47454650