0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ターミナルでポートを占有しているプロセスを一撃で強制終了するコマンド

0
Posted at

🎯 実行コマンド

ポート3000を占有しているプロセスを強制終了するコマンドです。

lsof -t -i :3000 | xargs kill -9

🔓 補足:他のポートの場合

ポート番号を変えるだけで、他のポートにも対応できます。

例:ポート8080を終了する場合

lsof -t -i :8080 | xargs kill -9

🧐 コマンドの解説

  1. lsof -i :3000:
    • lsof: List Open Files(開いているファイルの一覧表示)
    • -i :3000: ポート3000を使用しているプロセスを絞り込みます。
  2. lsof -t:
    • -t オプションは、通常表示されるプロセス情報から、プロセスID (PID) のみを抽出してリストで出力します。
  3. | (パイプ):
    • 左側のコマンドの出力(PIDのリスト)を、右側のコマンドの入力として渡します。
  4. xargs:
    • パイプで渡されたPIDのリストを、次のコマンド(kill -9)の引数として一つずつ展開し、実行します。
  5. kill -9:
    • kill コマンドでプロセスを終了させます。-9 はシグナル9(SIGKILL)を送信することを意味し、プロセスを強制的に終了させます。(最も確実な終了方法です)

⚠️ 安全確認 (Optional)

いきなり kill -9 を実行するのが不安な場合は、xargs kill -9 の代わりに xargs echo を使用して、どのPIDが対象になるかを事前に確認できます。

lsof -t -i :3000 | xargs echo

PIDのリストが表示されればOKです。確認後、本命のコマンドを実行してください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?