# 在 macOS 中找出監聽(佔用)某埠的 pid ###### tags: `macOS` `pid` `listen` `lsof` `cli` ## 實例 列出**監聽** TCP port 443 的 file(s): ``` $ lsof -iTCP:443 -sTCP:LISTEN ``` 結果可能如下: ``` COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 85560 pclin 90u IPv6 0x87025d221b224b65 0t0 TCP *:https (LISTEN) ``` 強制關閉: ``` $ kill 85560 ``` ## `lsof`參數 * `-i` > selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files…An Internet address is specified in the form (Items in square brackets are optional.): > > `[46][protocol][@hostname|hostaddr][:service|port]` > * `46` > IP 版本 > * `protocol` > 協定:`TCP`或`UDP` > * `hostname`或`hostaddr` > **主機名稱**或**主機位址** > * `service`|`port` > **服務名稱**<sub>(例如:ssl)</sub>或**埠號**<sub>(例如:443)</sub> * `-s` > inhibits the conversion of port numbers to port names for network files. Inhibiting the conversion may make lsof run a little faster. It is also useful when port name lookup is not working properly. > > `[p:s]` > * `p` > 協定:`TCP`或`UDP` > * `s` > 狀態:`ESTABLISHED`或`LISTEN`