安裝
brew install lsof
在Mac OS系統中,無法使用netstat來查看端口占用情況,可以使用lsof來代替,這種方式在Linux下也適用。
sudo lsof -nP -iTCP:端口號 -sTCP:LISTEN
-n 表示不顯示主機名
-P 表示不顯示端口俗稱
不加 sudo 只能查看以當前用戶運行的程序
查看指定端口以及對應的Command和PID:
lsof -nP -iTCP:50001 -sTCP:LISTEN
輸出
Python 1742 cxa 12u IPv4 0x81747a67e35c897 0t0 TCP *:50001 (LISTEN)
簡單的:
lsof -i:50001
觀察發現pid為1742,然后使用kill結束進程
kill -9 1742
