Shell – View current user process ancestors and formatting the output

linuxprocesspsshell

I'm using ps -eo ppid,pid,cmd to view all process, and how can I format the output to view like below?

Only given PID and its ancestor will print (until init).

23464   current
  |
23211   bash
  |
23210   sshd: xxW
  |
23193   sshd: WWcccv
  |
 728    /usr/sbin/sshd –D
  |
  1     init

I'm writing a script to view ancestral process using PID without using pstree, if possible.

Best Answer

Maybe something like:

#! /bin/bash -
pid=${1?Please give a pid}
sep=
while
  [ "$pid" -gt 0 ] &&
    read -r ppid name < <(ps -o ppid= -o args= -p "$pid")
do
  printf "$sep%$((3+${#pid}/2))d%$((4-${#pid}/2))s%s\n" "$pid" "" "$name"
  sep=$'  |\n'
  pid=$ppid
done

Which gives:

$ that-script "$$"
13612  /bin/zsh
  |
 4137  SCREEN screen -xRS main
  |
 4136  screen screen -xRS main
  |
 4130  zsh
  |
 4128  xterm
  |
  1    /sbin/init splash

That's a bit sluggish as it runs one ps per process in the ancestry. You can improve it by running ps to retrieve all the information at once and post process it with awk for instance:

#! /bin/sh -
pid=${1?Please give a pid}
ps -Ao pid= -o ppid= -o args= |
  awk -v p="$pid" '
    {
      pid = $1; ppid[pid] = $2
      sub(/([[:space:]]*[[:digit:]]+){2}[[:space:]]*/, "")
      args[pid] = $0
    }
    END {
      while (p) {
        printf "%s%*d%*s%s\n", sep, 3+length(p)/2, p, 4-length(p)/2, "", args[p]
        p = ppid[p]
        sep = "  |\n"
      }
    }'
Related Question