Is it possible to find the ports assigned to the process started on the background?

I want to start a process and let it run in the background, like ./run.sh & and then see all the ports this process is using.

This process might start other processes, so I'd like to see all ports locked by this process and any children.

Is it possible?

  • If I understand you correctly - netstat -nap may help you. And maybe a little script to find all child processes for your initial process, and then to search for network ports used by these (if any). – Jacek May 21 '15 at 15:56

It is possible. You could use netstat -a | egrep 'Proto|LISTEN'

  • It didn't work for me. I started a brand new terminal and without starting the run.sh I ran your script and it showed processes. Since I hadn't ran the process it should return nothing, no? – Alexandre Santos May 21 '15 at 16:07
  • netstat will return all processes. The | egrep 'Proto|LISTEN' is only filtering out processes that are not directly attached to a port. You could replace it with this line netstat -a | egrep 'run' or replace run with whatever service you're looking for. – Garheade May 21 '15 at 16:16
  • ok, but how do I filter it based on the pid of the process running in the background and its children? – Alexandre Santos May 21 '15 at 16:20
  • 1
    To get the pid included in netstat use the -p switch. netstat -ap | egrep '2134' Be sure to replace the '2134' with the pid you're actually searching for. – Garheade May 21 '15 at 16:30

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.