简体   繁体   English

杀死正在使用端口的PID

[英]Killing a PID which is using a port

I am using below code to kill a process which is using a port number 我正在使用下面的代码来杀死正在使用端口号的进程

port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'
if [ ! -z "$port" -a "$port" != " " ]; then
   sudo kill "$port"
fi

But it is saying port: command not found . 但这是说port: command not found What is causing the issue and how can I fix it. 是什么原因导致该问题以及如何解决。

As it stands, 就目前而言,

port = sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}'

attempts to run a command port with parameters = sudo lsof -n -i4TCP:3030 and pipe its output through grep LISTEN and then awk '{print $2;}' . 尝试运行参数= sudo lsof -n -i4TCP:3030的命令port ,并通过grep LISTEN然后awk '{print $2;}'传递其输出。

Use 采用

port=$(sudo lsof -n -i4TCP:3030 | grep LISTEN | awk '{print $2;}')

没有理由自己动手:Linux上的fuser将通过一个命令为您完成此任务,并且效率更高:

sudo fuser -n tcp -k 3030

只需一行!

sudo kill `sudo lsof -t -i:3030`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM