준호씨의 블로그

OSX - 8080 포트를 사용하는 프로세스를 찾아라. lsof, netstat 본문

개발이야기

OSX - 8080 포트를 사용하는 프로세스를 찾아라. lsof, netstat

준호씨 2020. 7. 5. 15:35

Spring Boot 프로젝트를 만들어서 돌리려고 하니

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

8080 포트를 이미 사용하고 있다고 합니다. 포트를 바꿔서 사용하면 되겠지만 8080 포트가 기본이다 보니 매번 포트 바꿔주기도 번거롭고 어디서 8080 포트를 사용하고 있는지 궁금하더군요.

 

lsof -list open files

가장 심플한 방법은 lsof를 사용하는 방법이었습니다.

$ lsof -nP -iTCP:8080 | grep LISTEN
java      66982 junho85   38u  IPv6 0x177016a9daa7a18b      0t0  TCP *:8080 (LISTEN)

얼마 전 kafka랑 같이 설치했던 zookeeper가 8080 포트를 사용하고 있었네요.

$ ps auxww | grep 66982
junho85          66982   0.0  0.2  9226736  53096   ??  S    25Jun20   7:21.00 /usr/bin/java -Dzookeeper.log.dir=/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../logs -Dzookeeper.log.file=zookeeper-junho85-server-JunHos-MacBook-Pro.local.log -Dzookeeper.root.logger=INFO,CONSOLE -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError=kill -9 %p -cp /usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../zookeeper-server/target/classes:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../build/classes:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../zookeeper-server/target/lib/*.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../build/lib/*.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/spotbugs-annotations-3.1.9.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-common-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/log4j-1.2.17.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jsr305-3.0.2.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jline-2.11.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-util-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-servlet-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-server-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-security-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-io-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jetty-http-9.4.17.v20190418.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jackson-databind-2.10.3.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jackson-core-2.10.3.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/jackson-annotations-2.10.3.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/commons-cli-1.2.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../zookeeper-3.5.8.jar:/usr/local/Cellar/zookeeper/3.5.8/libexec/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/etc/zookeeper: -Xmx1000m -Dapple.awt.UIElement=true -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /usr/local/etc/zookeeper/zoo.cfg

일단 zookeeper와 kafka는 사용할 때만 켜고 일단 껐습니다.

$ brew services stop kafka
Stopping `kafka`... (might take a while)
==> Successfully stopped `kafka` (label: homebrew.mxcl.kafka)

$ brew services stop zookeeper
Stopping `zookeeper`... (might take a while)
==> Successfully stopped `zookeeper` (label: homebrew.mxcl.zookeeper)

 

netstat

리눅스 환경에서는 보통 netstat 커맨드를 사용하는 편입니다.

$ netstat -ltnp | grep -w ':80' 

p 옵션이 process name이나 process id를 알려주기 때문입니다.

그런데 OSX에서는 p는 protocol입니다.

$ netstat -ltnp | grep -w ':80'
netstat: option requires an argument -- p
Usage:	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]

그래서 사용 방법이 좀 다릅니다.

$ netstat -p tcp -van | grep LISTEN | grep 8080
tcp46      0      0  *.8080                 *.*                    LISTEN      131072 131072  43227      0 0x0000 0x00000006

이렇게 하면 되는데요. -van옵션을 이용하면 pid도 볼 수 있습니다. 보이는 정보가 많아서 pid위치를 찾기가 좀 불편합니다.

아래처럼 항목 명도 보여주면 좀 낫습니다.

$ netstat -p tcp -van | grep LISTEN | grep 8080
tcp46      0      0  *.8080                 *.*                    LISTEN      131072 131072  43227      0 0x0000 0x00000006

$ netstat -p tcp -van | head -n2
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)     rhiwat shiwat    pid   epid  state    options

-van 옵션의 각각 설명은 아래와 같습니다.

     -v    Increase verbosity level.
     -a    With the default display, show the state of all sockets; normally sockets used by server processes are not shown. With the routing table display
           (option -r, as described below), show protocol-cloned routes (routes generated by a RTF_PRCLONING parent route); normally these routes are not shown.     
     -n    Show network addresses as numbers (normally netstat interprets addresses and attempts to display them symbolically).  This option may be used with any
           of the display formats.

-v로 정보를 좀 더 많이 나오도록 합니다. 이게 없으면 pid가 안 나옵니다.

$ netstat -p tcp -an | head -n2
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)

-a 가 없으면 LISTEN 상태인 내역이 나오지 않습니다.

-n 은 네트워크 주소를 숫자로 나오게 합니다. 포트번호도 숫자로 나오게 합니다. 이게 없으면 포트 번호로 grep을 할 수 없어서 -n은 꼭 넣어주시는 게 좋습니다. 숫자로 나오게 해야 결과도 빠르게 나오고요.

-n 이 있고 없고 차이는 아래를 보시면 되겠습니다.

$ netstat -p tcp -van
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)     rhiwat shiwat    pid   epid  state    options
tcp4       0      0  192.168.1.129.56813    52.114.133.60.443      ESTABLISHED 131072 131376  20920      0 0x0102 0x00020000
tcp4       0      0  192.168.1.129.56812    18.211.34.0.443        ESTABLISHED 131072 131768  27690      0 0x0102 0x00020000
...

$ netstat -p tcp -va
Active Internet connections (including servers)
Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)     rhiwat shiwat    pid   epid  state    options
tcp4       0      0  junhos-mbp.korne.57007 ec2-52-197-226-1.https ESTABLISHED 131631 131768  27690      0 0x0102 0x00020000
tcp4       0      0  junhos-mbp.korne.56813 52.114.133.60.https    ESTABLISHED 131072 131376  20920      0 0x0102 0x00020000
...

awk를 이용해서 좀 추려내면 아래처럼 됩니다.

$ netstat -p tcp -van | grep LISTEN | grep 8080 | awk '{print $4,$6,$9}'
*.8080 LISTEN 43227

 

결론적으로 osx에서 port로 process id를 찾을 때는 lsof가 훨씬 편리합니다.

 

0 Comments
댓글쓰기 폼