18

I would like to get a list of all the processes whose parent is $pid. This is the simplest way I've come up with:

pstree -p $pid | tr "\n" " " |sed "s/[^0-9]/ /g" |sed "s/\s\s*/ /g"

Is there any command, or any simpler way to get the list of children processes?

Thanks!

STenyaK
  • 725
  • 3
  • 7
  • 11

2 Answers2

22

Yes, using the -P option of pgrep,

i.e pgrep -P 1234 will get you a list of child process ids.

daisy
  • 53,527
  • 78
  • 236
  • 383
  • 3
    I'm afraid your answer is correct, but my question was incorrect. Therefore I accept your answer (which I didn't know, btw) and I opened another thread with the question I really meant to ask: http://unix.stackexchange.com/questions/67668/elegantly-get-list-of-descendant-processes – STenyaK Mar 12 '13 at 13:59
2

pids of all child processes of a given parent process <pid> id is present in /proc/<pid>/task/<tid>/children entry.

This file contains the pids of first-level child processes. Do it recursively for whole process tree.

head over to https://lwn.net/Articles/475688/ for more information.

y_159
  • 137
  • 1
  • 9