1

I have some pid and I want to know the process names for every parent all the way up to init.

For example when I type this command that i'm looking for I want to see something like "init───sshd───bash───mypidprocess"

By default pstree prints out the opposite, starting with the processname of your given pid it goes down all the way to the last child.

How would I tell pstree to recursively print out the parents of a pid?

1 Answers1

4

With GNU pstree, you can use the -H highlight option to highlight a process and all of its ancestors, and then just filter on the highlighting.

But with Fred Hucht's portable version of pstree, which is what you probably have on any platform but linux, there's really nothing that can help you. Of course you can parse the whole tree, but it would be easier to parse the ps output (which is what pstree itself does) directly. Or modify the (GPL) source to do what you want.

It might be even easier to write a simple program (in Python, C, whatever) that recursively calls getppid and then gets process info about the resulting parent.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I'm trying to log the result of this command somewhere, so having the whole tree printed out won't work in my case, thanks though. –  May 24 '13 at 20:39
  • @RodrigoSalazar: Where did I suggest having the whole tree printed out? – abarnert May 24 '13 at 20:39
  • hmm I just tried pstree -H , and I get a large tree with the path i'm interested in highlighted. Maybe my pstree behaves differently? –  May 24 '13 at 20:40
  • @RodrigoSalazar: "you can use the `-H` highlight option to highlight a process and all of its ancestors **and then just filter on the highlighting**". Did you not read that part? – abarnert May 24 '13 at 20:44
  • How do you filter on highlighting? –  May 24 '13 at 20:50
  • Well, highlighting is done by sending terminal control characters. Those are just characters, so you can filter on them the same way you would with anything. The only possible problem is that you may need to trick pstree into believing it's talking to a terminal rather than a filter. But at that point, I think this is more a question for ServerFault, SuperUser, LinuxQuestions, AskUbuntu, or some similar site, because it really has nothing to do with programming. – abarnert May 24 '13 at 21:16