用途
port-process 用于端口与进程管理,解决的问题包括:查找某个端口正在被什么进程占用、终止卡住或残留的进程、以及查看系统的 CPU、内存、磁盘和文件描述符等资源使用情况。当用户提到「port in use」「what's on port」「kill process」「lsof」等关键词时适用。
主要能力
查找端口占用
- macOS 与 Linux:
lsof -i :3000,是跨平台最可靠的首选。 - Linux(更快):
ss -tlnp | grep :3000。 - 较老 Linux 系统:
netstat -tlnp | grep :3000。 - 查看所有监听端口:
lsof -i -P -n | grep LISTEN或ss -tlnp。
资料说明:lsof 在 macOS 和 Linux 上都可用;Linux 上 ss 更快,优先于已废弃的 netstat。
终止进程
- 按 PID:
kill 12345(SIGTERM,优雅退出)、kill -9 12345(SIGKILL,强制,最后手段)。 - 按端口:
lsof -ti :3000 | xargs kill、lsof -ti :3000 | xargs kill -9。 - 按名称:
pkill node、pkill -f "next dev"、killall node。 lsof 的-t标志只输出 PID,便于通过管道传给 kill。
macOS 与 Linux 差异
资料给出对照:端口查询 macOS 用 lsof -i :PORT,Linux 用 lsof -i :PORT 或 ss -tlnp;按端口终止 macOS 用 lsof -ti :PORT | xargs kill,Linux 用 fuser -k PORT/tcp;进程树 macOS 用 pstree PID(需通过 brew 安装),Linux 用 pstree -p PID 或 ps --forest;网络统计 macOS 用 netstat -an(无 -p 标志),Linux 用 ss -tlnp 或 netstat -tlnp;文件描述符 macOS 用 lsof -p PID,Linux 用 ls /proc/PID/fd 或 lsof -p PID。macOS 上不存在 ss 和 fuser,应坚持使用 lsof。
“Address Already in Use” 诊断流程
遇到 EADDRINUSE 时:1) lsof -i :3000 找到占用者;2) lsof -i :3000 -sTCP:LISTEN 确认是否为自身残留进程;3) ps -p <PID> -o pid,ppid,user,command 查看详情;4) 先优雅 kill <PID>;5) lsof -i :3000 验证是否释放;6) 仍卡住则 kill -9 <PID>;7) 若端口处于 TIME_WAIT(Linux),可在代码中加入 SO_REUSEADDR 或等待约 60 秒,用 ss -tlnp | grep :3000 查看。若进程的 PPID 为 1,说明已成孤儿进程,可直接终止。
按名称查找进程
ps aux | grep node、ps aux | grep -v grep | grep node;更优方式:pgrep -la node、pgrep -f "next dev"、pidof node(仅 Linux)。查看已知 PID 详情:ps -p 12345 -o pid,ppid,user,%cpu,%mem,etime,command。
进程树
Linux:pstree -p 12345、ps -ejH、ps --forest -eo pid,ppid,cmd;macOS:pstree 12345(需 brew install pstree)。查找父进程:ps -o ppid= -p 12345。用于定位是哪个 shell 或 supervisor 启动了失控进程。
后台与前台
node server.js & 后台运行;jobs 列出后台任务;fg %1 调至前台;先 Ctrl-Z 挂起再 bg %1 转后台;nohup node server.js > output.log 2>&1 & 脱离终端;对已在后台的任务用 disown %1 让 shell 不再因退出发送 SIGHUP。
资源使用
top、htop 交互监控;按 CPU 排序:Linux ps aux --sort=-%cpu | head -20,macOS ps aux -r | head -20;按内存排序:Linux ps aux --sort=-%mem | head -20,macOS ps aux -m | head -20;内存概览:Linux free -h,macOS vm_stat(页数除以 4096 得字节)。
磁盘使用
df -h 查看文件系统;du -sh /path/to/dir 单目录大小;du -h /path | sort -rh | head -10 找最大目录;ncdu /path 交互式浏览;find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null 找大文件;Docker 占用:docker system df、docker system prune -a。
文件描述符
ulimit -n 查看软限制,ulimit -Hn 查看硬限制,ulimit -n 65536 提升当前会话软限制;lsof -p 12345 列出某进程打开的文件,lsof -p 12345 | wc -l 计数;Linux 可直接 ls /proc/12345/fd | wc -l。若进程触及描述符上限,会报 “too many open files”,需提高 ulimit -n 或修复文件/套接字泄漏。
信号
资料列出:SIGTERM(15) 优雅关闭,进程可捕获清理,默认,永远先试;SIGINT(2) 同 Ctrl-C;SIGHUP(1) 挂起,部分守护进程收到后重载配置(nginx、Apache);SIGKILL(9) 立即终止,不可捕获;SIGSTOP(19) 暂停进程;SIGCONT(18) 恢复被暂停的进程。始终先发 SIGTERM 并等待几秒,只在其拒绝退出时才用 kill -9;SIGKILL 会跳过所有清理,遗留临时文件、锁文件和套接字。
常见场景
- 开发服务器端口被占:
lsof -ti :3000 | xargs kill后重启服务器。 - 孤立 Node 进程占用 CPU:
pgrep -la node按命令行识别残留进程后kill <PID>。 - 多个残留开发服务器:
pkill -f "next dev"、pkill -f "vite"、pkill -f "react-scripts"(生产环境慎用)。 - 磁盘空间不足:
df -h确认卷,du -h /home --max-depth=2 | sort -rh | head -20找大目录,常见可清理项包括docker system prune -a、rm -rf node_modules/.cache、npm cache clean --force。 - 僵尸进程:
ps aux | awk '$8=="Z"'查找,僵尸无法直接杀死,需终止其父进程:ps -o ppid= -p <ZOMBIE_PID>后kill <PARENT_PID>,由 init/systemd 回收。
使用方式
该技能以命令行操作为主,用户按上述命令在终端执行即可。资料未提供图形界面、自动化封装、额外安装包或插件形式的使用说明。
适用场景
适用于本地开发调试、服务器运维排查、进程与资源诊断等命令行环境,尤其适合 macOS 与 Linux 用户处理端口冲突和进程管理问题。
限制
资料未说明该技能的收费、价格、支持平台版本、权限要求或性能数据;macOS 上 ss 与 fuser 不可用,pstree 需另行安装;部分命令(如 pkill、kill -9、docker system prune)具有破坏性,资料提醒在生产环境中谨慎使用。