简体   繁体   中英

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 . 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;}' .

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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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