How to write a shell script for printing the PID and The owner and the Name of the Process using the Special Variable $$ as parameter for the skript

Solution 1:

ps -o ppid will output the pid of the parent process. So, start with the current pid, and ask for the parent, then for its parent, and so on.

#! /bin/bash
pid=$1
while ((pid)) ; do
    ps -h -o 'pid,comm,euser' $pid \
        | sed -E 's/^( *[0-9]+ )([^ ]+) *([^ ]+)/\1(\2,\3)/'
    pid=$(ps -h -o ppid $pid)
done

You want to output pid, the command, and the effective user (or maybe real user?). Specify them in the -o and use sed to reformat the output. Here, we capture the three non-space strings and insert parentheses and a comma where needed.