2
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?

More than 5 years have passed since last update.

指定したポートを占有しているプロセスをKILLするシェル書いた

Last updated at Posted at 2018-05-09

最近lsofでKILLすること増えて、lsofいちいちするのが面倒になったので。

https://stackoverflow.com/questions/9168392/shell-script-to-kill-the-process-listening-on-port-3000
の内容を引数指定にしました。

# !/bin/bash
# @see https://stackoverflow.com/questions/9168392/shell-script-to-kill-the-process-listening-on-port-3000
pid=$(lsof -i:$1 -t)
echo ${pid}
if [ -z "${pid}" ]; then
  echo "port: '$1' not found. quit."
  exit 1
fi  
kill -TERM $pid || kill -KILL $pid

これをパスの通ったところに(私は ~/bin とかにパスを通しています) killport とか適当な名前で保存して755のpermissionとかすればいいよね。使う時は以下のように。lsofで前後確認するとちゃんとKILLされたのがわかる

$ lsof -i:3000 
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby    70638  ms2sato    9u  IPv4 0xe754e6b893fe4a91      0t0  TCP *:hbci (LISTEN)
$ killport 3000
70638
$ lsof -i:3000 
$
2
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
2
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?