19

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

24

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
    Commented Mar 12, 2013 at 13:59
3

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.

2
  • 1
    Presence of the file is governed by CONFIG_PROC_CHILDREN. Commented Mar 29, 2021 at 7:24
  • 1
    Unreliable as per man7.org/linux/man-pages/man5/proc.5.html, "[...] reliably provides a list of children only if all of the child processes are stopped or frozen."
    – TheDiveO
    Commented Jan 6 at 16:24

You must log in to answer this question.

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