Solaris ptree style tool for Linux












8















I'm looking for the Linux tool that will print the same output as the Solaris ptree.
For example:



# ptree 538
538 /usr/lib/ssh/sshd
889 /usr/lib/ssh/sshd
890 /usr/lib/ssh/sshd
1498 -sh
1649 bash
1656 -sh
1660 bash
13716 ptree 538


I'm aware that pstree exists, but I don't like its output format. Does anyone know any similar tools?










share|improve this question















migrated from serverfault.com Sep 3 '11 at 22:17


This question came from our site for system and network administrators.




















    8















    I'm looking for the Linux tool that will print the same output as the Solaris ptree.
    For example:



    # ptree 538
    538 /usr/lib/ssh/sshd
    889 /usr/lib/ssh/sshd
    890 /usr/lib/ssh/sshd
    1498 -sh
    1649 bash
    1656 -sh
    1660 bash
    13716 ptree 538


    I'm aware that pstree exists, but I don't like its output format. Does anyone know any similar tools?










    share|improve this question















    migrated from serverfault.com Sep 3 '11 at 22:17


    This question came from our site for system and network administrators.


















      8












      8








      8


      3






      I'm looking for the Linux tool that will print the same output as the Solaris ptree.
      For example:



      # ptree 538
      538 /usr/lib/ssh/sshd
      889 /usr/lib/ssh/sshd
      890 /usr/lib/ssh/sshd
      1498 -sh
      1649 bash
      1656 -sh
      1660 bash
      13716 ptree 538


      I'm aware that pstree exists, but I don't like its output format. Does anyone know any similar tools?










      share|improve this question
















      I'm looking for the Linux tool that will print the same output as the Solaris ptree.
      For example:



      # ptree 538
      538 /usr/lib/ssh/sshd
      889 /usr/lib/ssh/sshd
      890 /usr/lib/ssh/sshd
      1498 -sh
      1649 bash
      1656 -sh
      1660 bash
      13716 ptree 538


      I'm aware that pstree exists, but I don't like its output format. Does anyone know any similar tools?







      linux ps






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 4 '11 at 22:25









      Gilles

      531k12810631591




      531k12810631591










      asked Sep 3 '11 at 22:10









      EldarEldar

      66114




      66114




      migrated from serverfault.com Sep 3 '11 at 22:17


      This question came from our site for system and network administrators.






      migrated from serverfault.com Sep 3 '11 at 22:17


      This question came from our site for system and network administrators.
























          4 Answers
          4






          active

          oldest

          votes


















          7














          This is all I know that is most like ptree in linux



          ps -ejH





          share|improve this answer



















          • 3





            Or ps axf. I find it more readable.

            – manatwork
            Sep 4 '11 at 9:47











          • or even ps axf -o pid,command

            – jlliagre
            Sep 4 '11 at 15:56



















          5














          Here's a script that shows output similar to Solaris pstree. No option is supported, and user matching is not supported. This script should be portable to all POSIX systems. On some systems whose ps command isn't POSIX-compliant, you may need to adjust the options passed to ps. The script includes specific support for BSD systems, so most platforms should be covered.



          #! /bin/sh
          ## Usage: $0 [PID...]
          ## Show the processes on the system. For each process, show the process
          ## id followed by the command line. Show child processes after their parent,
          ## indented.
          ## If one or more PIDs are specified, only show the ancestors and
          ## descendants of those PIDs. If no PID is specified, show the subtree
          ## rooted at PID 1.
          ## This utility mimics Solaris pstree(1).
          case $(uname) in *BSD*) ps_A='-ax';; *) ps_A='-A';; esac
          ps $ps_A -o pid= -o ppid= -o args= |
          sort -k 1n |
          awk -v targets="$*" '
          # children[p]: the " "-separated list of the pids of the children of p
          # cmd[p]: command line of p
          # list[lb..le]: list of pids yet to traverse
          # depth[p]: depth of process p: depth(child) = depth(parent) + 1
          # parent[p]: pid of the parent of p
          # show[p]: 1 to show p, 2 to show p and all its descendants
          BEGIN {
          list[0] = 0; lb = 0; le = 0;
          depth[0] = -1;
          }
          {
          pid=$1; ppid=$2;
          sub(/^ *[0-9]+ +[0-9]+ /, "");
          if (pid == ppid) {
          # This process is a root: add it to the list of processes to taverse
          list[++le] = pid;
          } else {
          children[ppid] = children[ppid] " " pid;
          parent[pid] = ppid;
          }
          cmd[pid] = $0;
          }
          END {
          # Parse targets into a list of pids (or 1 if none is specified).
          split("_" targets, a, /[^0-9]+/);
          delete a[1];
          if (a[2] == "") a[2] = 1;
          for (i in a) {
          show[a[i]] = 2; # Show targets recursively
          p = parent[a[i]];
          # Show target ancestors
          while (p && !show[p]) {
          show[p] = 1;
          p = parent[p];
          }
          }

          # Traverse the list of processes
          while (lb <= le) {
          pid = list[lb++];
          # Add children to the list of processes to traverse
          split(children[pid], a);
          for (i in a) {
          list[--lb] = a[i];
          depth[a[i]] = depth[pid] + 1;
          if (show[pid] > 1) show[a[i]] = show[pid];
          }
          # Show the current process if desired, indenting to the right depth
          if (show[pid]) {
          for (i = 1; i <= depth[pid]; i++) printf(" ");
          printf("%-5d ", pid);
          print cmd[pid];
          }
          }
          }
          '





          share|improve this answer

































            2














            This probably isn't what you're looking for exactly, but others might appreciate it.



            htop has a tree view if you press F5.






            share|improve this answer































              0














              Perhaps pstree does fit the bill?






              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "106"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20078%2fsolaris-ptree-style-tool-for-linux%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                7














                This is all I know that is most like ptree in linux



                ps -ejH





                share|improve this answer



















                • 3





                  Or ps axf. I find it more readable.

                  – manatwork
                  Sep 4 '11 at 9:47











                • or even ps axf -o pid,command

                  – jlliagre
                  Sep 4 '11 at 15:56
















                7














                This is all I know that is most like ptree in linux



                ps -ejH





                share|improve this answer



















                • 3





                  Or ps axf. I find it more readable.

                  – manatwork
                  Sep 4 '11 at 9:47











                • or even ps axf -o pid,command

                  – jlliagre
                  Sep 4 '11 at 15:56














                7












                7








                7







                This is all I know that is most like ptree in linux



                ps -ejH





                share|improve this answer













                This is all I know that is most like ptree in linux



                ps -ejH






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 3 '11 at 23:03









                MikeMike

                45748




                45748








                • 3





                  Or ps axf. I find it more readable.

                  – manatwork
                  Sep 4 '11 at 9:47











                • or even ps axf -o pid,command

                  – jlliagre
                  Sep 4 '11 at 15:56














                • 3





                  Or ps axf. I find it more readable.

                  – manatwork
                  Sep 4 '11 at 9:47











                • or even ps axf -o pid,command

                  – jlliagre
                  Sep 4 '11 at 15:56








                3




                3





                Or ps axf. I find it more readable.

                – manatwork
                Sep 4 '11 at 9:47





                Or ps axf. I find it more readable.

                – manatwork
                Sep 4 '11 at 9:47













                or even ps axf -o pid,command

                – jlliagre
                Sep 4 '11 at 15:56





                or even ps axf -o pid,command

                – jlliagre
                Sep 4 '11 at 15:56













                5














                Here's a script that shows output similar to Solaris pstree. No option is supported, and user matching is not supported. This script should be portable to all POSIX systems. On some systems whose ps command isn't POSIX-compliant, you may need to adjust the options passed to ps. The script includes specific support for BSD systems, so most platforms should be covered.



                #! /bin/sh
                ## Usage: $0 [PID...]
                ## Show the processes on the system. For each process, show the process
                ## id followed by the command line. Show child processes after their parent,
                ## indented.
                ## If one or more PIDs are specified, only show the ancestors and
                ## descendants of those PIDs. If no PID is specified, show the subtree
                ## rooted at PID 1.
                ## This utility mimics Solaris pstree(1).
                case $(uname) in *BSD*) ps_A='-ax';; *) ps_A='-A';; esac
                ps $ps_A -o pid= -o ppid= -o args= |
                sort -k 1n |
                awk -v targets="$*" '
                # children[p]: the " "-separated list of the pids of the children of p
                # cmd[p]: command line of p
                # list[lb..le]: list of pids yet to traverse
                # depth[p]: depth of process p: depth(child) = depth(parent) + 1
                # parent[p]: pid of the parent of p
                # show[p]: 1 to show p, 2 to show p and all its descendants
                BEGIN {
                list[0] = 0; lb = 0; le = 0;
                depth[0] = -1;
                }
                {
                pid=$1; ppid=$2;
                sub(/^ *[0-9]+ +[0-9]+ /, "");
                if (pid == ppid) {
                # This process is a root: add it to the list of processes to taverse
                list[++le] = pid;
                } else {
                children[ppid] = children[ppid] " " pid;
                parent[pid] = ppid;
                }
                cmd[pid] = $0;
                }
                END {
                # Parse targets into a list of pids (or 1 if none is specified).
                split("_" targets, a, /[^0-9]+/);
                delete a[1];
                if (a[2] == "") a[2] = 1;
                for (i in a) {
                show[a[i]] = 2; # Show targets recursively
                p = parent[a[i]];
                # Show target ancestors
                while (p && !show[p]) {
                show[p] = 1;
                p = parent[p];
                }
                }

                # Traverse the list of processes
                while (lb <= le) {
                pid = list[lb++];
                # Add children to the list of processes to traverse
                split(children[pid], a);
                for (i in a) {
                list[--lb] = a[i];
                depth[a[i]] = depth[pid] + 1;
                if (show[pid] > 1) show[a[i]] = show[pid];
                }
                # Show the current process if desired, indenting to the right depth
                if (show[pid]) {
                for (i = 1; i <= depth[pid]; i++) printf(" ");
                printf("%-5d ", pid);
                print cmd[pid];
                }
                }
                }
                '





                share|improve this answer






























                  5














                  Here's a script that shows output similar to Solaris pstree. No option is supported, and user matching is not supported. This script should be portable to all POSIX systems. On some systems whose ps command isn't POSIX-compliant, you may need to adjust the options passed to ps. The script includes specific support for BSD systems, so most platforms should be covered.



                  #! /bin/sh
                  ## Usage: $0 [PID...]
                  ## Show the processes on the system. For each process, show the process
                  ## id followed by the command line. Show child processes after their parent,
                  ## indented.
                  ## If one or more PIDs are specified, only show the ancestors and
                  ## descendants of those PIDs. If no PID is specified, show the subtree
                  ## rooted at PID 1.
                  ## This utility mimics Solaris pstree(1).
                  case $(uname) in *BSD*) ps_A='-ax';; *) ps_A='-A';; esac
                  ps $ps_A -o pid= -o ppid= -o args= |
                  sort -k 1n |
                  awk -v targets="$*" '
                  # children[p]: the " "-separated list of the pids of the children of p
                  # cmd[p]: command line of p
                  # list[lb..le]: list of pids yet to traverse
                  # depth[p]: depth of process p: depth(child) = depth(parent) + 1
                  # parent[p]: pid of the parent of p
                  # show[p]: 1 to show p, 2 to show p and all its descendants
                  BEGIN {
                  list[0] = 0; lb = 0; le = 0;
                  depth[0] = -1;
                  }
                  {
                  pid=$1; ppid=$2;
                  sub(/^ *[0-9]+ +[0-9]+ /, "");
                  if (pid == ppid) {
                  # This process is a root: add it to the list of processes to taverse
                  list[++le] = pid;
                  } else {
                  children[ppid] = children[ppid] " " pid;
                  parent[pid] = ppid;
                  }
                  cmd[pid] = $0;
                  }
                  END {
                  # Parse targets into a list of pids (or 1 if none is specified).
                  split("_" targets, a, /[^0-9]+/);
                  delete a[1];
                  if (a[2] == "") a[2] = 1;
                  for (i in a) {
                  show[a[i]] = 2; # Show targets recursively
                  p = parent[a[i]];
                  # Show target ancestors
                  while (p && !show[p]) {
                  show[p] = 1;
                  p = parent[p];
                  }
                  }

                  # Traverse the list of processes
                  while (lb <= le) {
                  pid = list[lb++];
                  # Add children to the list of processes to traverse
                  split(children[pid], a);
                  for (i in a) {
                  list[--lb] = a[i];
                  depth[a[i]] = depth[pid] + 1;
                  if (show[pid] > 1) show[a[i]] = show[pid];
                  }
                  # Show the current process if desired, indenting to the right depth
                  if (show[pid]) {
                  for (i = 1; i <= depth[pid]; i++) printf(" ");
                  printf("%-5d ", pid);
                  print cmd[pid];
                  }
                  }
                  }
                  '





                  share|improve this answer




























                    5












                    5








                    5







                    Here's a script that shows output similar to Solaris pstree. No option is supported, and user matching is not supported. This script should be portable to all POSIX systems. On some systems whose ps command isn't POSIX-compliant, you may need to adjust the options passed to ps. The script includes specific support for BSD systems, so most platforms should be covered.



                    #! /bin/sh
                    ## Usage: $0 [PID...]
                    ## Show the processes on the system. For each process, show the process
                    ## id followed by the command line. Show child processes after their parent,
                    ## indented.
                    ## If one or more PIDs are specified, only show the ancestors and
                    ## descendants of those PIDs. If no PID is specified, show the subtree
                    ## rooted at PID 1.
                    ## This utility mimics Solaris pstree(1).
                    case $(uname) in *BSD*) ps_A='-ax';; *) ps_A='-A';; esac
                    ps $ps_A -o pid= -o ppid= -o args= |
                    sort -k 1n |
                    awk -v targets="$*" '
                    # children[p]: the " "-separated list of the pids of the children of p
                    # cmd[p]: command line of p
                    # list[lb..le]: list of pids yet to traverse
                    # depth[p]: depth of process p: depth(child) = depth(parent) + 1
                    # parent[p]: pid of the parent of p
                    # show[p]: 1 to show p, 2 to show p and all its descendants
                    BEGIN {
                    list[0] = 0; lb = 0; le = 0;
                    depth[0] = -1;
                    }
                    {
                    pid=$1; ppid=$2;
                    sub(/^ *[0-9]+ +[0-9]+ /, "");
                    if (pid == ppid) {
                    # This process is a root: add it to the list of processes to taverse
                    list[++le] = pid;
                    } else {
                    children[ppid] = children[ppid] " " pid;
                    parent[pid] = ppid;
                    }
                    cmd[pid] = $0;
                    }
                    END {
                    # Parse targets into a list of pids (or 1 if none is specified).
                    split("_" targets, a, /[^0-9]+/);
                    delete a[1];
                    if (a[2] == "") a[2] = 1;
                    for (i in a) {
                    show[a[i]] = 2; # Show targets recursively
                    p = parent[a[i]];
                    # Show target ancestors
                    while (p && !show[p]) {
                    show[p] = 1;
                    p = parent[p];
                    }
                    }

                    # Traverse the list of processes
                    while (lb <= le) {
                    pid = list[lb++];
                    # Add children to the list of processes to traverse
                    split(children[pid], a);
                    for (i in a) {
                    list[--lb] = a[i];
                    depth[a[i]] = depth[pid] + 1;
                    if (show[pid] > 1) show[a[i]] = show[pid];
                    }
                    # Show the current process if desired, indenting to the right depth
                    if (show[pid]) {
                    for (i = 1; i <= depth[pid]; i++) printf(" ");
                    printf("%-5d ", pid);
                    print cmd[pid];
                    }
                    }
                    }
                    '





                    share|improve this answer















                    Here's a script that shows output similar to Solaris pstree. No option is supported, and user matching is not supported. This script should be portable to all POSIX systems. On some systems whose ps command isn't POSIX-compliant, you may need to adjust the options passed to ps. The script includes specific support for BSD systems, so most platforms should be covered.



                    #! /bin/sh
                    ## Usage: $0 [PID...]
                    ## Show the processes on the system. For each process, show the process
                    ## id followed by the command line. Show child processes after their parent,
                    ## indented.
                    ## If one or more PIDs are specified, only show the ancestors and
                    ## descendants of those PIDs. If no PID is specified, show the subtree
                    ## rooted at PID 1.
                    ## This utility mimics Solaris pstree(1).
                    case $(uname) in *BSD*) ps_A='-ax';; *) ps_A='-A';; esac
                    ps $ps_A -o pid= -o ppid= -o args= |
                    sort -k 1n |
                    awk -v targets="$*" '
                    # children[p]: the " "-separated list of the pids of the children of p
                    # cmd[p]: command line of p
                    # list[lb..le]: list of pids yet to traverse
                    # depth[p]: depth of process p: depth(child) = depth(parent) + 1
                    # parent[p]: pid of the parent of p
                    # show[p]: 1 to show p, 2 to show p and all its descendants
                    BEGIN {
                    list[0] = 0; lb = 0; le = 0;
                    depth[0] = -1;
                    }
                    {
                    pid=$1; ppid=$2;
                    sub(/^ *[0-9]+ +[0-9]+ /, "");
                    if (pid == ppid) {
                    # This process is a root: add it to the list of processes to taverse
                    list[++le] = pid;
                    } else {
                    children[ppid] = children[ppid] " " pid;
                    parent[pid] = ppid;
                    }
                    cmd[pid] = $0;
                    }
                    END {
                    # Parse targets into a list of pids (or 1 if none is specified).
                    split("_" targets, a, /[^0-9]+/);
                    delete a[1];
                    if (a[2] == "") a[2] = 1;
                    for (i in a) {
                    show[a[i]] = 2; # Show targets recursively
                    p = parent[a[i]];
                    # Show target ancestors
                    while (p && !show[p]) {
                    show[p] = 1;
                    p = parent[p];
                    }
                    }

                    # Traverse the list of processes
                    while (lb <= le) {
                    pid = list[lb++];
                    # Add children to the list of processes to traverse
                    split(children[pid], a);
                    for (i in a) {
                    list[--lb] = a[i];
                    depth[a[i]] = depth[pid] + 1;
                    if (show[pid] > 1) show[a[i]] = show[pid];
                    }
                    # Show the current process if desired, indenting to the right depth
                    if (show[pid]) {
                    for (i = 1; i <= depth[pid]; i++) printf(" ");
                    printf("%-5d ", pid);
                    print cmd[pid];
                    }
                    }
                    }
                    '






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 5 '11 at 18:47

























                    answered Sep 4 '11 at 22:24









                    GillesGilles

                    531k12810631591




                    531k12810631591























                        2














                        This probably isn't what you're looking for exactly, but others might appreciate it.



                        htop has a tree view if you press F5.






                        share|improve this answer




























                          2














                          This probably isn't what you're looking for exactly, but others might appreciate it.



                          htop has a tree view if you press F5.






                          share|improve this answer


























                            2












                            2








                            2







                            This probably isn't what you're looking for exactly, but others might appreciate it.



                            htop has a tree view if you press F5.






                            share|improve this answer













                            This probably isn't what you're looking for exactly, but others might appreciate it.



                            htop has a tree view if you press F5.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 4 '11 at 0:56









                            Shawn J. GoffShawn J. Goff

                            29.4k19110134




                            29.4k19110134























                                0














                                Perhaps pstree does fit the bill?






                                share|improve this answer




























                                  0














                                  Perhaps pstree does fit the bill?






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Perhaps pstree does fit the bill?






                                    share|improve this answer













                                    Perhaps pstree does fit the bill?







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 23 '13 at 15:49









                                    vonbrandvonbrand

                                    14.2k22644




                                    14.2k22644






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20078%2fsolaris-ptree-style-tool-for-linux%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                        is 'sed' thread safe

                                        How to make a Squid Proxy server?