pstree but without other children of ancestors

Hi, I want to display the process tree of a given PID, however, I don't want to see other children of the ancestors that don't reach the current PID.

My goal is, from the tree result, i have to fetch a particular parent process by keyword, and if any other children from parents have the same name, it will give me incorrect results

I used 'pstree -alhpA <pid>' but it displays the other children of the parents too

Is there a easy way to achieve this without having to write a custom script to loop thru each PPID and reach the root?

Thanks,
-sri

You can probably do it by implementing your own logic: "-f" will give you a output format of "ps" where not only the PID but also the PPID (parent PID) is listed. With this information you can build your own (sub-)tree.

From a certain parent PID search for every process with this PID in the PPID field to get all the children, then repeat this for every child recursively to get your subsection of the tree. If you don't find any children to a process it is one of the "leaves" of the tree and the recursion ends.

The only thing you need to know at the beginning is the PID of the "root" process you want to start your tree with.

I hope this helps.

bakunin

Thanks bakunin, I was hoping there was a built-in utility (similar to pstree) that would accomplish my requirement. I will go ahead and write custom code to navigate from current pid to the root