pstree does not show every process

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.

Asked By: new_x

||

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
Answered By: steeldriver

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
Answered By: Raffa
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.