shell脚本杀死监听端口3000的进程?
我想定义一个名为 kill3000
的 bash 别名来自动执行以下任务:
$ lsof -i:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 13402 zero 4u IPv4 2847851 0t0 TCP *:3000 (LISTEN)
$ kill -9 13402
I want to define a bash alias named kill3000
to automate the following task:
$ lsof -i:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 13402 zero 4u IPv4 2847851 0t0 TCP *:3000 (LISTEN)
$ kill -9 13402
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
-t 标志就是你想要的:它显示 PID,而不是其他任何东西。
更新
如果找不到该进程并且您不想看到错误消息:
假设您正在运行 bash。
更新
Basile的建议是优秀:我们应该首先尝试终止进程,通常会
kill -TERM
,如果失败,则kill -KILL
(又名kill -9
):您可能希望将其设为 bash 函数。
Try this:
The -t flag is what you want: it displays PID, and nothing else.
Update
In case the process is not found and you don't want to see error message:
Assuming you are running bash.
Update
Basile's suggestion is excellent: we should first try to terminate the process normally will
kill -TERM
, if failed, thenkill -KILL
(AKAkill -9
):You might want to make this a bash function.
使用原始 lsof 命令的另一个选项:
如果您想在 shell 脚本中使用它,您可以将
-r
标志添加到xargs
处理没有进程正在监听的情况:Another option using using the original
lsof
command:If you want to use this in a shell script, you could add the
-r
flag toxargs
to handle the case where no process is listening:fuser -k 3000/tcp
也应该可以工作fuser -k 3000/tcp
should also work怎么样
How about
将产生输出
所以你可以这样做:
Will yield the output of
So you could do: