126

Resolved before asked: cat /proc/1111/status | grep PPid

Vi.
  • 17,755

11 Answers11

140

Command line:

ps -o ppid= -p 1111

Function:

ppid () { ps -p ${1:-$$} -o ppid=; }

Alias (a function is preferable):

alias ppid='ps -o ppid= -p'

Script:

#!/bin/sh
pid=$1
if [ -z $pid ]
then
    read -p "PID: " pid
fi
ps -p ${pid:-$$} -o ppid=

If no PID is supplied to the function or the script, they default to show the PPID of the current process.

To use the alias, a PID must be supplied.

21

To print parent ids (PPID) of all the processes, use this command:

ps j

For the single process, just pass the PID, like: ps j 1234.

To extract only the value, filter output by awk, like:

ps j | awk 'NR>1 {print $3}' # BSD ps
ps j | awk 'NR>1 {print $1}' # GNU ps

To list PIDs of all parents, use pstree (install it if you don't have it):

$ pstree -sg 1234
systemd(1)───sshd(1036)───bash(2383)───pstree(3007)

To get parent PID of the current process, use echo $$.

kenorb
  • 26,615
15

This is one of those things I learn, forget, relearn, repeat. But it's useful. The pstree command's ‘s’ flag shows a tree with a leaf at N:

pstree -sA $(pgrep badblocks)
systemd---sudo---mkfs.ext4---badblocks
14

Parent pid is in shell variable PPID, so

echo $PPID
Giacomo1968
  • 58,727
Ivan Novotny
  • 149
  • 1
  • 2
10

Read /proc/$PID/status. Can be easily scripted:

#!/bin/sh
P=$1
if [ -z "$P" ]; then
    read P
fi
cat /proc/"$P"/status | grep PPid: | grep -o "[0-9]*"
Vi.
  • 17,755
5

On Linux:

ps hoppid $thatprocess
Giacomo1968
  • 58,727
jthill
  • 161
3
$ ps -p $(ps -p $(echo $$) -o ppid=) -o comm=
    tmux

A little bit more complex example that checks the command of a parent that started current process Change comm= to cmd= to see full command

2

Run top with whatever options you want, like -u username and -p PID.

And while top is working press f, it shows a list of options you want to display in top output, and the displayed parameters will be shown in CAPITAL letters and the parameters which or not displaying will be shown in small letters.

So by entering the letter before the parameter you can enable or disable it. For parent process ID you have to enter b and then press Enter, it'll display the PPID in top output.

Kevin Panko
  • 7,466
1

all parent processes of a pid

I came here when I was trying to find "all parent processes of a pid". I ended up making my own recursive function to do it.

pid_lineage.sh

#!/bin/bash -eu

main(){ ps --pid ${1:-$$} --no-headers --format pid,ppid,args |
( read pid ppid args echo -e "$pid\t$args" [[ $pid -gt 1 ]] && main $ppid ) }

main "$@"

1

Here is a quick solution that should also work:

ps $$
0

Adding another way to do the same thing with ps. It seems this is sometimes the only way to get it done on certain Linux-based embedded devices.

ps -o ppid= -o pid= | grep MYPID

first column is the parent PID, second column is the child.