我试图在命令行中为ubuntu中的特定端口终止进程。

如果我运行此命令,我将获得端口:

sudo lsof -t -i:9001

所以…现在我想跑:

sudo kill 'sudo lsof -t -i:9001'

我收到以下错误消息:

ERROR: garbage process ID "lsof -t -i:9001".
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

我也尝试过杀“lsof-t-I:9001”


当前回答

要终止在端口号9001上运行的进程

sudo kill -9 $(sudo lsof -t -i:9001)


lsof   - list of files(Also used for to list related processes)

-t     - show only process ID

-i     - show only internet connections related process

:9001  - show only processes in this port number

kill   - command to kill the process

-9     - forcefully

sudo   - command to ask admin privilege(user id and password).

有关更多信息,请访问我的博客

其他回答

使用killport命令:

sh killport 9001 

要下载shell,可以使用wget:

wget https://cdn.rawgit.com/abdennour/miscs.sh/e0aac343/killport

你可以使用

fuser -n tcp -k 9001 

在维基百科中查看更多详细信息

也可以使用xargs。只需运行以下命令,

sudo lsof-t-i:443|xargs sudo kill-9

killport <port>

在CLI中添加此代码并使用此别名。

killport () {
  PID=$(sudo lsof -t -i:$1)
  sudo kill -9 ${PID}
}

xargs是一个非常有用的命令。

您可以使用这样的管道命令

lsof -t -i :9001 | xargs sudo kill

它的作用:它接受第一个命令的输出,在本例中是在端口上运行的进程,并将其传递给sudokill。