shell脚本杀死进程监听端口3000?(复制)

时间:2022-02-07 08:22:52

This question already has an answer here:

这个问题已经有了答案:

I want to define a bash alias named kill3000 to automate the following task:

我想定义一个名为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

6 个解决方案

#1


92  

alias kill3000="fuser -k -n tcp 3000"

#2


40  

Try this:

试试这个:

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

The -t flag is what you want: it displays PID, and nothing else.

您想要的是-t标志:它显示PID,而不是其他任何东西。

UPDATE

In case the process is not found and you don't want to see error message:

如果没有找到过程,您不想看到错误消息:

    kill -9 $(lsof -i:3000 -t) 2> /dev/null

Assuming you are running bash.

假设您正在运行bash。

UPDATE

Basile's suggestion is excellent: we should first try to terminate the process normally will kill -TERM, if failed, then kill -KILL (AKA kill -9):

Basile的建议很好:我们应该先尝试终止这个过程,通常会杀死-TERM,如果失败,然后杀死-KILL (AKA kill -9):

    pid=$(lsof -i:3000 -t); kill -TERM $pid || kill -KILL $pid

You might want to make this a bash function.

您可能想让它成为bash函数。

#3


8  

Another option using using the original lsof command:

使用原始lsof命令的另一个选项:

lsof -n -i:3000 | grep LISTEN | awk '{ print $2 }' | uniq | xargs kill -9

If you want to use this in a shell script, you could add the -r flag to xargs to handle the case where no process is listening:

如果您想在shell脚本中使用它,您可以将-r标志添加到xargs,以处理没有进程正在侦听的情况:

... | xargs -r kill -9

#4


2  

fuser -n tcp 3000

Will yield the output of

将产生的输出

3000/tcp:     <$pid>

So you could do:

所以你可以做的:

fuser -n tcp 3000 | awk '{ print $2 }' | xargs -r kill

#5


2  

fuser -k 3000/tcp should also work

fuser - k3000 /tcp也可以工作

#6


1  

How about

如何

alias kill3000="lsof -i:3000 | grep LISTEN | awk '{print $2}' | xargs kill -9"

#1


92  

alias kill3000="fuser -k -n tcp 3000"

#2


40  

Try this:

试试这个:

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

The -t flag is what you want: it displays PID, and nothing else.

您想要的是-t标志:它显示PID,而不是其他任何东西。

UPDATE

In case the process is not found and you don't want to see error message:

如果没有找到过程,您不想看到错误消息:

    kill -9 $(lsof -i:3000 -t) 2> /dev/null

Assuming you are running bash.

假设您正在运行bash。

UPDATE

Basile's suggestion is excellent: we should first try to terminate the process normally will kill -TERM, if failed, then kill -KILL (AKA kill -9):

Basile的建议很好:我们应该先尝试终止这个过程,通常会杀死-TERM,如果失败,然后杀死-KILL (AKA kill -9):

    pid=$(lsof -i:3000 -t); kill -TERM $pid || kill -KILL $pid

You might want to make this a bash function.

您可能想让它成为bash函数。

#3


8  

Another option using using the original lsof command:

使用原始lsof命令的另一个选项:

lsof -n -i:3000 | grep LISTEN | awk '{ print $2 }' | uniq | xargs kill -9

If you want to use this in a shell script, you could add the -r flag to xargs to handle the case where no process is listening:

如果您想在shell脚本中使用它,您可以将-r标志添加到xargs,以处理没有进程正在侦听的情况:

... | xargs -r kill -9

#4


2  

fuser -n tcp 3000

Will yield the output of

将产生的输出

3000/tcp:     <$pid>

So you could do:

所以你可以做的:

fuser -n tcp 3000 | awk '{ print $2 }' | xargs -r kill

#5


2  

fuser -k 3000/tcp should also work

fuser - k3000 /tcp也可以工作

#6


1  

How about

如何

alias kill3000="lsof -i:3000 | grep LISTEN | awk '{print $2}' | xargs kill -9"