I need to write lsof output for several network ports into bash variable.

Simple $(lsof -i :5555) doesn't work - lsof waits for quit command (Ctrl-C) every time I call it.

I can't figure out how to solve my task.

  • 1
    More details, e.g. what OS? What happens if you run lsof as a single command? It works here on Ubuntu 12.04 and CentOS 6.5.
    – Sven
    Apr 3 '14 at 13:03
  • 1
    Usually when lsof takes a long time to complete it is because DNS resolution takes too long. Could that be it? Try adding -n
    – faker
    Apr 3 '14 at 13:07
  • @VictorMezrin great, I've added it as an answer
    – faker
    Apr 3 '14 at 14:49

A long running lsof process usually means that DNS resolution is timing out or not working correctly which is delaying the output of it.
You can disable DNS resolution by adding the -n option.

Of course you might want to check out why DNS resolution is taking too long on your server as well.

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.