显示HN:Sonar – 一个小型CLI,用于查看和终止localhost上运行的任何进程
Show HN: Sonar – A tiny CLI to see and kill whatever's running on localhost

原始链接: https://github.com/RasKrebs/sonar

## Sonar:本地端口管理工具 Sonar是一个命令行工具,旨在快速识别和管理监听在本地主机的进程。厌倦了手动检查端口冲突? Sonar会显示所有正在监听的服务,包括Docker容器和Compose项目,以及资源使用情况、URL和容器名称等详细信息。 **主要特性:** * **端口列表:** 使用`sonar list`轻松列出所有活动端口,可以按Docker过滤、排序或以JSON格式输出。 * **进程控制:** 终止进程 (`sonar kill`)、查看日志 (`sonar attach`),甚至可以进入Docker容器。 * **实时监控:** `sonar watch`提供端口状态和资源使用情况的实时更新。 * **连接可视化:** `sonar graph`显示服务之间的已建立连接。 * **项目配置:** 使用`sonar profile`定义和管理项目的预期端口。 **安装:** 一个简单的`curl`命令即可将Sonar安装到`~/.local/bin`。它支持shell补全以方便使用。 Sonar通过提供对正在运行的服务全面概览以及强大的管理工具,简化了本地开发。

## Sonar:一个本地端口管理CLI 开发者 raskrebs 创建了 **Sonar**,一个轻量级的命令行工具,用于轻松识别和终止监听在localhost上的进程。由于反复使用复杂的 `lsof` 命令来解决来自多个本地项目和Docker容器的端口冲突而感到沮丧,他们构建了 Sonar 作为一种简单易记的解决方案。 Sonar 会显示正在运行的进程,并提供有用的信息,如进程名称和Docker容器详情,并允许用户使用简单的命令来终止它们(例如,`sonar kill 3000`)。 虽然存在 `lsof` 的别名等替代方案,但 raskrebs 提到代理编排工具对此感兴趣,并认为 Sonar 在新兴的代理编码领域具有潜力,尤其是在进程退出后管理容器方面。该项目是开源的,欢迎贡献和反馈。
相关文章

原文
███████╗ ██████╗ ███╗   ██╗ █████╗ ██████╗
██╔════╝██╔═══██╗████╗  ██║██╔══██╗██╔══██╗
███████╗██║   ██║██╔██╗ ██║███████║██████╔╝
╚════██║██║   ██║██║╚██╗██║██╔══██║██╔══██╗
███████║╚██████╔╝██║ ╚████║██║  ██║██║  ██║
╚══════╝ ╚═════╝ ╚═╝  ╚═══╝╚═╝  ╚═╝╚═╝  ╚═╝

Know what's running on your machine.

I got tired of running lsof -iTCP -sTCP:LISTEN | grep ... every time a port was already taken, then spending another minute figuring out if it was a Docker container or some orphaned dev server from another worktree. So I built sonar.

It shows everything listening on localhost, with Docker container names, Compose projects, resource usage, and clickable URLs. You can kill processes, tail logs, shell into containers, and more — all by port number.

$ sonar list
PORT   PROCESS                      CONTAINER                    IMAGE             CPORT   URL
1780   proxy (traefik:3.0)          my-app-proxy-1               traefik:3.0       80      http://localhost:1780
3000   next-server (v16.1.6)                                                               http://localhost:3000
5432   db (postgres:17)             my-app-db-1                  postgres:17       5432    http://localhost:5432
6873   frontend (frontend:latest)   my-app-frontend-1            frontend:latest   5173    http://localhost:6873
9700   backend (backend:latest)     my-app-backend-1             backend:latest    8000    http://localhost:9700

5 ports (4 docker, 1 user)
curl -sfL https://raw.githubusercontent.com/raskrebs/sonar/main/install.sh | bash

Downloads the latest binary to ~/.local/bin and adds it to your PATH if needed. Restart your terminal or source ~/.zshrc.

Custom install location:

curl -sfL https://raw.githubusercontent.com/raskrebs/sonar/main/install.sh | SONAR_INSTALL_DIR=/usr/local/bin bash

Shell completions (tab-complete port numbers):

sonar completion zsh > "${fpath[1]}/_sonar"   # zsh
sonar completion bash > /etc/bash_completion.d/sonar  # bash
sonar completion fish | source                 # fish
sonar list                     # show all ports
sonar list --stats             # include CPU, memory, state, uptime
sonar list --filter docker     # only Docker ports
sonar list --sort name         # sort by process name
sonar list --json              # JSON output
sonar list -a                  # include desktop apps
sonar list -c port,cpu,mem,uptime,state  # custom columns
sonar list --health            # run HTTP health checks
sonar list --host user@server  # scan a remote machine via SSH

By default, sonar hides desktop apps and system services that listen on TCP ports but aren't relevant to development — things like Figma, Discord, Spotify, ControlCenter, AirPlay, and other macOS .app bundles and /System/Library/ daemons. Use -a to include them.

Available columns: port, process, pid, type, url, cpu, mem, threads, uptime, state, connections, health, latency, container, image, containerport, compose, project, user, bind, ip

Shows everything about a port: full command, user, bind address, CPU/memory/threads, uptime, health check result, and Docker details if applicable.

sonar kill 3000                            # SIGTERM
sonar kill 3000 -f                         # SIGKILL
sonar kill-all --filter docker             # stop all Docker containers
sonar kill-all --project my-app            # stop a Compose project
sonar kill-all --filter user -y            # skip confirmation

Docker containers are stopped with docker stop instead of sending signals.

For Docker containers, runs docker logs -f. For native processes, discovers log files via lsof and tails them. Falls back to macOS log stream or Linux /proc/<pid>/fd.

sonar attach 3000                          # shell into Docker container, or TCP connect
sonar attach 3000 --shell bash             # specific shell
sonar watch                                # poll every 2s, show diffs
sonar watch --stats                        # live resource stats (like docker stats)
sonar watch -i 500ms                       # faster polling
sonar watch --notify                       # desktop notifications when ports go up/down
sonar watch --host user@server             # watch a remote machine
sonar graph                                # show which services talk to each other
sonar graph --json                         # structured output
sonar graph --dot                          # Graphviz DOT format

Shows established connections between listening ports (e.g. your backend connecting to postgres).

Save a set of expected ports for a project, then check if they're all up or tear them down:

sonar profile create my-app                # snapshot current ports
sonar profile list                         # list saved profiles
sonar profile show my-app                  # show profile details
sonar up my-app                            # check which expected ports are running
sonar down my-app                          # stop all ports in the profile

Proxies traffic so the service on port 6873 is also available on port 3002.

sonar open 3000                            # open in browser
sonar tray                                 # menu bar app with live stats (macOS)
sonar --no-color                           # disable colors (also respects NO_COLOR env)

The --stats flag fetches per-process and per-container resource usage. For Docker containers, it uses the Docker Engine API for accurate per-container metrics. Without --stats, sonar returns instantly.

  • macOS (uses lsof)
  • Linux (uses ss)
联系我们 contact @ memedata.com