Score:4

pstree does not show every process

gf flag

I opened a terminal made sudo su run the nano editor in the background by running it as nano&.

I tried to find the newly started nano process in the process list.
To do that, I typed

ps aux|grep -i nano 

-> Saw the newly created nano

but when I tried to see the same nano process with pstree, it did not list the nano. I used

pstree -p|grep -i nano

No output was shown.

Raffa avatar
jp flag
`pstree -ap | grep -i nano` ... with `-a` should show it.
Score:6
hr flag

It appears that pstree truncates long lines when piped e.g.

# pstree -sp "$$"
systemd(1)───sddm(1007)───sddm-helper(1097)───lxqt-session(1114)───qterminal(1475)───bash(1478)───sudo(28929)───sudo(28930)───su(28931)───bash(28932)─┬─nano(28938)                          
                                                                                                                                                      └─pstree(29086)

but

# pstree -sp "$$" | cat                                                                                                                   
systemd(1)---sddm(1007)---sddm-helper(1097)---lxqt-session(1114)---qterminal(1475)---bash(1478)---sudo(28929)---sudo(28930)---su(28+

(notice everything after sudo(28930)---su(28+ is omitted).

You can pass the -l or --long option to prevent that, i.e.

# pstree -lsp "$$" | grep -i nano                                                                                                           
                                                                                                                                                      |-nano(28938)

or avoid the issue by not starting all the way from PID 1 - for example, starting from the parent shell's PID:

# pstree -p "$$" | grep -i nano
            |-nano(28938)

or (perhaps better) avoid grepping the pstree output (which is really intended for human eyes) by using pgrep instead:

# pgrep -af nano
28938 nano
Raffa avatar
jp flag
What a coincidence :) +1
hr flag
@Raffa indeed ;-p
new_x avatar
gf flag
Thank both of you. Since Rafta is the first I chose his answer. But thank you steeldriver for your super explanation
David Z avatar
es flag
+1 for the suggestion of `pgrep`
Score:3
jp flag

pstree through a pipe .. Check it with:

pstree -p | tee

might not show the full command/command-line arguments depending on how far to the right side of the tree a process is ... It might show a partial command followed by a plus sign indicating the output is shortened like --nan+ instead of --nano or might not even show that if that process is far enough to the right side of the processes tree.

Therefore, if you need to show command line arguments, then use the option for that which is -a:

pstree -ap | grep -i nano

Although grep is more suitable to ps than it is to pstree as the "tree" part will be lost ... If you have to use pstree with grep then at least use grep's -B and -A options to show a few lines before and after the match so that the "tree" is at least partially shown like so:

pstree -ap | grep -B 5 -A 5 -i nano
Unmitigated avatar
jp flag
`-B 5 -A 5` can be replaced with `-C 5`.
Raffa avatar
jp flag
@Unmitigated Thanks for your comment ... That is correct as well as alternatively using only a dash `-` and a number i.e. `-5` at least on Ubuntu's implementation of GNU grep, but `-B` and `-A` offer more control of course.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.