根据关键字定位问题的方式有很多,通过 ps -ef | grep 即可,我们还希望能够通过进程ID查看其占用的端口,实际操作过程中,发现 Mac 下使用 netstat 不能执行。
linux 使用 ps -ef | grep *** 定位 PID [admin@test test.inc.alipay.net /home/admin]$ ps -ef | grep linkcadmin 60828 58930 0 18:07 pts/1 00:00:00 grep --color=auto linkcadmin 71090 1 0 13:58 ? 00:00:00 /xhdxh/sh /home/admin/linkc-run/linkcadmin 71101 71090 3 13:58 ? 00:09:55 /opt/taobao/java/xhdxh/java -server -Xdebug 。。。。 使用 netstat -nltp | grep PID 查看进程占用的所有端口 $ netstat -nltp | grep 71101(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)tcp 0 0 0.0.0.0:43016 0.0.0.0:* LISTEN 71101/javatcp 0 0 0.0.0.0:5005 0.0.0.0:* LISTEN 71101/javatcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 71101/javatcp 0 0 0.0.0.0:8083 0.0.0.0:* LISTEN 71101/javatcp 0 0 0.0.0.0:8084 0.0.0.0:* LISTEN 71101/java Mac在 Mac 下,同样的 netstat 命令执行无效,替换使用 lsof -nP -iTCP -sTCP:LISTEN | grep PID 查看
➜ logstash netstat -nltp | grep 56238netstat: option requires an argument -- pUsage:netstat [-AaLlnW] [-f address_family | -p protocol]netstat [-gilns] [-f address_family]netstat -i | -I interface [-w wait] [-abdgRtS]netstat -s [-s] [-f address_family | -p protocol] [-w wait]netstat -i | -I interface -s [-f address_family | -p protocol]netstat -m [-m]netstat -r [-Aaln] [-f address_family]netstat -rs [-s]➜ linkc git:(feature/permission_change_log) ✗ lsof -nP -iTCP -sTCP:LISTEN | grep 55546java 55546 huke 463u IPv6 0x5a58b4fec38ff955 0t0 TCP *:51529 (LISTEN)java 55546 huke 466u IPv6 0x5a58b4fec3901bd5 0t0 TCP *:51506 (LISTEN)java 55546 huke 467u IPv6 0x5a58b4fec39032d5 0t0 TCP *:51530 (LISTEN)java 55546 huke 551u IPv6 0x5a58b4fec3369f15 0t0 TCP *:8083 (LISTEN)java 55546 huke 559u IPv6 0x5a58b4fec336bbd5 0t0 TCP *:8081 (LISTEN)java 55546 huke 567u IPv6 0x5a58b4fedd8b7bd5 0t0 TCP *:8082 (LISTEN)java 55546 huke 574u IPv6 0x5a58b4fedd8b5f15 0t0 TCP *:8080 (LISTEN)