How can I get only the ancestry processes of a given process?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












Is it correct that



  • pstree <pid> will output all the descendant processes of the given process


  • pstree -s <pid> will output all the descendant processes and ancestry processes of the given process


How can I get only the ancestry processes of a given process?



Thanks.







share|improve this question





















  • similar: View current user process ancestors and formating the output
    – Stéphane Chazelas
    Jul 28 at 8:56
















up vote
0
down vote

favorite












Is it correct that



  • pstree <pid> will output all the descendant processes of the given process


  • pstree -s <pid> will output all the descendant processes and ancestry processes of the given process


How can I get only the ancestry processes of a given process?



Thanks.







share|improve this question





















  • similar: View current user process ancestors and formating the output
    – Stéphane Chazelas
    Jul 28 at 8:56












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Is it correct that



  • pstree <pid> will output all the descendant processes of the given process


  • pstree -s <pid> will output all the descendant processes and ancestry processes of the given process


How can I get only the ancestry processes of a given process?



Thanks.







share|improve this question













Is it correct that



  • pstree <pid> will output all the descendant processes of the given process


  • pstree -s <pid> will output all the descendant processes and ancestry processes of the given process


How can I get only the ancestry processes of a given process?



Thanks.









share|improve this question












share|improve this question




share|improve this question








edited Jul 29 at 4:48
























asked Jul 28 at 1:09









Tim

22.5k61222398




22.5k61222398











  • similar: View current user process ancestors and formating the output
    – Stéphane Chazelas
    Jul 28 at 8:56
















  • similar: View current user process ancestors and formating the output
    – Stéphane Chazelas
    Jul 28 at 8:56















similar: View current user process ancestors and formating the output
– Stéphane Chazelas
Jul 28 at 8:56




similar: View current user process ancestors and formating the output
– Stéphane Chazelas
Jul 28 at 8:56










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You can always walk the ancestry tree by hand using ps -o ppid=:



#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done


Or to avoid running ps several times:



#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '

pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0

END
while (p)
print p, name[p]
p = ppid[p]

'





share|improve this answer























  • Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
    – Tim
    Jul 29 at 17:43










  • @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
    – Stéphane Chazelas
    Jul 29 at 18:56










  • Thanks. (1) Can both scripts use bash in shebang?
    – Tim
    Jul 29 at 19:01


















up vote
2
down vote













You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.






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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f458973%2fhow-can-i-get-only-the-ancestry-processes-of-a-given-process%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    You can always walk the ancestry tree by hand using ps -o ppid=:



    #! /bin/bash -
    pid=$1?Please give a pid
    while
    [ "$pid" -gt 0 ] &&
    read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
    do
    printf '%sn' "$pid $name"
    pid=$ppid
    done


    Or to avoid running ps several times:



    #! /bin/sh -
    pid=$1?Please give a pid
    ps -Ao pid= -o ppid= -o comm= |
    awk -v p="$pid" '

    pid = $1; ppid[pid] = $2
    sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
    name[pid] = $0

    END
    while (p)
    print p, name[p]
    p = ppid[p]

    '





    share|improve this answer























    • Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
      – Tim
      Jul 29 at 17:43










    • @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
      – Stéphane Chazelas
      Jul 29 at 18:56










    • Thanks. (1) Can both scripts use bash in shebang?
      – Tim
      Jul 29 at 19:01















    up vote
    4
    down vote



    accepted










    You can always walk the ancestry tree by hand using ps -o ppid=:



    #! /bin/bash -
    pid=$1?Please give a pid
    while
    [ "$pid" -gt 0 ] &&
    read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
    do
    printf '%sn' "$pid $name"
    pid=$ppid
    done


    Or to avoid running ps several times:



    #! /bin/sh -
    pid=$1?Please give a pid
    ps -Ao pid= -o ppid= -o comm= |
    awk -v p="$pid" '

    pid = $1; ppid[pid] = $2
    sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
    name[pid] = $0

    END
    while (p)
    print p, name[p]
    p = ppid[p]

    '





    share|improve this answer























    • Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
      – Tim
      Jul 29 at 17:43










    • @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
      – Stéphane Chazelas
      Jul 29 at 18:56










    • Thanks. (1) Can both scripts use bash in shebang?
      – Tim
      Jul 29 at 19:01













    up vote
    4
    down vote



    accepted







    up vote
    4
    down vote



    accepted






    You can always walk the ancestry tree by hand using ps -o ppid=:



    #! /bin/bash -
    pid=$1?Please give a pid
    while
    [ "$pid" -gt 0 ] &&
    read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
    do
    printf '%sn' "$pid $name"
    pid=$ppid
    done


    Or to avoid running ps several times:



    #! /bin/sh -
    pid=$1?Please give a pid
    ps -Ao pid= -o ppid= -o comm= |
    awk -v p="$pid" '

    pid = $1; ppid[pid] = $2
    sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
    name[pid] = $0

    END
    while (p)
    print p, name[p]
    p = ppid[p]

    '





    share|improve this answer















    You can always walk the ancestry tree by hand using ps -o ppid=:



    #! /bin/bash -
    pid=$1?Please give a pid
    while
    [ "$pid" -gt 0 ] &&
    read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
    do
    printf '%sn' "$pid $name"
    pid=$ppid
    done


    Or to avoid running ps several times:



    #! /bin/sh -
    pid=$1?Please give a pid
    ps -Ao pid= -o ppid= -o comm= |
    awk -v p="$pid" '

    pid = $1; ppid[pid] = $2
    sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
    name[pid] = $0

    END
    while (p)
    print p, name[p]
    p = ppid[p]

    '






    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jul 29 at 9:33


























    answered Jul 28 at 8:06









    Stéphane Chazelas

    278k52511841




    278k52511841











    • Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
      – Tim
      Jul 29 at 17:43










    • @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
      – Stéphane Chazelas
      Jul 29 at 18:56










    • Thanks. (1) Can both scripts use bash in shebang?
      – Tim
      Jul 29 at 19:01

















    • Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
      – Tim
      Jul 29 at 17:43










    • @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
      – Stéphane Chazelas
      Jul 29 at 18:56










    • Thanks. (1) Can both scripts use bash in shebang?
      – Tim
      Jul 29 at 19:01
















    Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
    – Tim
    Jul 29 at 17:43




    Thanks. (1) Is there some reason to use bash in the shebang of one script, and sh in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/…
    – Tim
    Jul 29 at 17:43












    @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
    – Stéphane Chazelas
    Jul 29 at 18:56




    @Tim, for (1) that's because <(...) is not sh syntax. That will only work in zsh, bash or recent versions of ksh93. ... | read -r pid name would only work in zsh or ksh93, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
    – Stéphane Chazelas
    Jul 29 at 18:56












    Thanks. (1) Can both scripts use bash in shebang?
    – Tim
    Jul 29 at 19:01





    Thanks. (1) Can both scripts use bash in shebang?
    – Tim
    Jul 29 at 19:01













    up vote
    2
    down vote













    You can try following, I found it in Linux man page :
    -h
    This highlight the current process and its ancestors.
    -n
    This will sort processes with the same ancestor by PID instead of by name.






    share|improve this answer

























      up vote
      2
      down vote













      You can try following, I found it in Linux man page :
      -h
      This highlight the current process and its ancestors.
      -n
      This will sort processes with the same ancestor by PID instead of by name.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        You can try following, I found it in Linux man page :
        -h
        This highlight the current process and its ancestors.
        -n
        This will sort processes with the same ancestor by PID instead of by name.






        share|improve this answer













        You can try following, I found it in Linux man page :
        -h
        This highlight the current process and its ancestors.
        -n
        This will sort processes with the same ancestor by PID instead of by name.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 28 at 3:30









        Nur

        214




        214






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458973%2fhow-can-i-get-only-the-ancestry-processes-of-a-given-process%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?