본문 바로가기

일지/문제해결일지

[Shell] 한 줄로 포트 번호에 해당하는 프로세스 죽이기

How to kill a process by port number with one line?

 

배포 자동화와 관련된 업무를 하던 중, 포트 번호에 해당하는 프로세스를 죽이는 스크립트 작성이 필요했습니다.

이미 구 버전의 서버가 8080포트에서 실행중인데 다음 버전의 서버를 또 8080포트에 열면

에러가 발생해 배포에 실패하기 때문입니다.

 

단순히 lsof 명령어, netstat 명령어를 통해 port number에 해당하는 pid를 알아내

kill {pid} 하면 되지만, 스크립트를 통한 배포 자동화였기에 하나의 명령어 라인으로 프로세스를 죽이는 방법이 필요했습니다.

 

제가 찾아낸 방법은 두 가지입니다. 첫 번째는 fuser를 사용하는 방식, 두 번째는 lsof를 사용하는 방식입니다.

fuser -k 8080/tcp
kill $(lsof -t -i:8080)

 

 

 

 

How to kill a process running on particular port in Linux?

I tried to close the tomcat using ./shutdown.sh from tomcat /bin directory. But found that the server was not closed properly. And thus I was unable to restartMy tomcat is running on port 8080. I ...

stackoverflow.com

 

한 줄은 아니지만 포트번호에 해당하는 프로세스를 죽이는 방법

 

How to kill a process running on particular port in Linux?

I tried to close the tomcat using ./shutdown.sh from tomcat /bin directory. But found that the server was not closed properly. And thus I was unable to restartMy tomcat is running on port 8080. I ...

stackoverflow.com