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!

2 Answers 2

22

Yes, using the -P option of pgrep,

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

1
  • 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: unix.stackexchange.com/questions/67668/…
    – STenyaK
    Mar 12, 2013 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.

1
  • 1
    Presence of the file is governed by CONFIG_PROC_CHILDREN. Mar 29, 2021 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .