0

Each time I need to stop the process that is occupying port 8080, I have to run this command first

$ lsof -i:8080

to get the PID

COMMAND  PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
python  4024 ubuntu    3u  IPv4 2181080      0t0  TCP *:http-alt (LISTEN) 

and then I kill the PID

kill 4024

I'd like to write a script to make it simple, how do I extract the PID from the output of lsof -i:8080?

1

You can pipe the lsof -i:8080 output to awk to get the PID:

lsof -i:8080 | awk 'NR==2 {print $2}'

You can then combine it with kill:

kill $(lsof -i:8080 | awk 'NR==2 {print $2}')

You can also create a Bash function and add it at the end of your .bashrc:

function kill8080()
{
    kill $(lsof -i:8080 | awk 'NR==2 {print $2}')
}

I named the above function kill8080, but you can name it as you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.