18
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

1つのシンプルなコマンドで特定のPortのプロセスをkillする方法

Last updated at Posted at 2023-04-18

結論

image.png

kill-portというnpmパッケージを使用する。

使い方

8080のPortのプロセスをkillしたい場合

$ npx kill-port 8080

複数のPortのプロセスをkillしたい場合

$ npx kill-port 9000 3000 5000

通常のやり方

ポートのプロセスを調べる

$ lsof -i :8080

COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    36411 oekazuma   28u  IPv6 0xa9c4356a4d10c577      0t0  TCP localhost:8080 (LISTEN)

PIDをコピーし、プロセスをkillする

$ kill -9 36411

このやり方をやってる方が多いんじゃないでしょうか?
僕も今までこのやり方をやってました。
ただ、最初にPIDを調べて、そのIDをコピーしてkillコマンドを叩くって結構面倒です。

$ kill -9 $(lsof -t -i:8080)

こんな感じで一行で書くやり方もありますが、ちょっとコマンドが長いですし、覚えるのが大変ですのでkill-portを使うのは有用そうです。

18
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?