how to get all parent processes and all subprocesses by `pstree`

后端 未结 2 491
耶瑟儿~
耶瑟儿~ 2021-02-05 13:36

command pstree PID can show all subprocess information of the process specified by PID. However, I also want to know all parent process information of

相关标签:
2条回答
  • 2021-02-05 14:12

    I found laps options mentioned by @haridsv (pstree -laps <pid>) being a solution. It was a bit verbose for me though, so I'd stick to a shorter aps output.

    0 讨论(0)
  • 2021-02-05 14:27
    # With my psmisc 22.20:
    pstree -p -s PID
    

    Maybe if with ps -ef:

    awk -vPID=$1 '
    function getParent ( pid ) {
        if (pid == "" || pid == "0") return;
        while ("ps -ef | grep "pid | getline) {
            if ($2 == pid) {
                print $8"("$2") Called By "$3;
                getParent($3);
                break;
            }
        }
        close ("ps -ef")
    }
    
    BEGIN { getParent(PID) }
    '
    

    This is ugly assuming ps output column and order. Actually one single run of ps -ef contains all info needed. This don't worth the time, I still recommend updating psmisc, it won't hurt.

    EDIT: A mimic using single run ps -ef:

    ps -ef | awk -vPID=$1 '
    function getpp ( pid, pcmd, proc ) {
        for ( p in pcmd ) {
            if (p == pid) {
                getpp(proc[p], pcmd, proc);
                if (pid != PID) printf("%s(%s)───", pcmd[pid], pid);
            }
        }
    }
    
    NR > 1 {
        # pid=>cmd
        pcmd[$2] = $8;
        # pid=>Parent
        pproc[$2] = $3;
    }
    
    END {
        getpp(PID, pcmd, pproc);
        printf "\n";
        system("pstree -p "PID);
    }'
    
    0 讨论(0)
提交回复
热议问题