Skip to content
Advertisement

Grandchildren and Great-grandchildren of a process

I am trying to get the grandchildren and grand grandchildren PID’S of a certain PID that I pass as an argument.

So, I am trying to find the simplest solution possible for a beginner. Now, I used pgrep -P $pid, to find the children of a certain PID, but now that I want to go even deeper, I think I might need to use a data pipe and filter out more stuff out of it.

pgrep -P $pid | grep something

but how do I go deeper into the descendent of these children? is there a way to reuse the pgrep -P once again, but this time on the result I got?

P.S

I researched in this forum before about this, but all solutions are quite advanced for me and I don’t really understand them. Would even love to know if possible to have a solution only using ps -P without the pgrep.

Advertisement

Answer

You can do it easily using xargs:

pgrep -P $pid | xargs -n1 pgrep -P

that would give you the grandchildren. xargs will take the output of the first command and pass it one by one (-n1) to the second command

using only ps:

ps --ppid $pid -o pid --no-headers | xargs -n1 ps --no-headers --ppid

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement