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!

STenyaK
  • 745

2 Answers2

23

Yes, using the -P option of pgrep,

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

daisy
  • 54,555
  • 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
  • 1
    Presence of the file is governed by CONFIG_PROC_CHILDREN. – Irfan Latif Mar 29 '21 at 07:24
  • Unreliable as per https://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 Jan 06 '24 at 16:24