0

I have been trying to kill a process by looking at the listening port and nothing seems to be working. The code is as follows:

(code to open port 8545) && fuser -k 8545/tcp

I also tried with kill -9 $(lsof -t -i:8545) and lsof -ti:8545 | xargs kill.

The line that did work was kill -9 $(lsof -i :8545 | awk '$8 == "TCP" { print $2 }'), which, afaik, is searching for the PID and killing that (as opposed to the TCP connection.

Why can I not kill it with fuser or the other methods mentioned?

Note: I tried it with a sleep 5 (much longer than the process takes to open), and that did not work either.

  • 3
    because fuser is doing the lookup before that code had the chance to open the port? It would help if you gave a real example instead of pseudocode -- in your example, fuser won't run at all until the (code bla bla ...) has returned with a successful status. – pizdelect Feb 9 at 6:09
  • Even with the &&? I tried it with a sleep 5 (much longer than the process takes to open), and that did not work either. – python_crypto_questions Feb 9 at 6:14
  • Not "even with", because of it ... i – tink Feb 9 at 6:18
  • 2
    this should work. nc -l 8545 & sleep 1; fuser -k 8545/tcp. Notice the single &. If you have something more special in that (code ...), share. – pizdelect Feb 9 at 6:22

Your Answer

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

Browse other questions tagged or ask your own question.