How to get parent PID of a given process in GNU/Linux from command line?

94

42

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

Vi.

Posted 2010-06-08T09:42:29.587

Reputation: 13 705

faster: grep PPid status |cut -f2 like in time(for((i=0;i<1000;i++));do grep PPid status |cut -f2 >/dev/null;done); wonder if there is something even faster? – Aquarius Power – 2014-08-09T23:55:06.670

1@AquariusPower Since you ask, fgrep is faster than grep. fgrep PPid status |cut -f2 – jbo5112 – 2016-02-18T22:46:25.110

sed is way faster than grep and cut: sed -rn '/PPid/ s/^.*:\s+// p' < status – Marian – 2017-04-25T23:15:13.480

Answers

112

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.

Paused until further notice.

Posted 2010-06-08T09:42:29.587

Reputation: 86 075

The = sign is not necessary, at least on OS X 10.8.2. – jtbandes – 2013-01-09T19:04:48.543

6

@jtbandes: The equal sign as used here suppresses the output of the header line (Linux and OS X).

– Paused until further notice. – 2013-01-09T21:08:20.423

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

user194394

Posted 2010-06-08T09:42:29.587

Reputation: 151

13

Parent pid is in shell variable PPID, so

echo $PPID

Ivan Novotny

Posted 2010-06-08T09:42:29.587

Reputation: 131

1Yes, but 1. I want parent pid of other process, 2. I want to be able to traverse all ancestors to init. – Vi. – 2012-09-24T12:37:33.807

1On the other hand, using $PPID did just solve the problem I had which Google suggested this page as an answer to. – Paul Whittaker – 2012-09-24T15:58:26.210

12

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

Posted 2010-06-08T09:42:29.587

Reputation: 16 795

1pstree is the nicest one I've seen here. – sudo – 2017-09-08T00:33:23.350

ps j is great because it's available on many distros and is easily composable – Connor McCormick – 2019-07-22T20:58:50.580

6

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.

Posted 2010-06-08T09:42:29.587

Reputation: 13 705

2UUOC useless use of cat – Felipe Alvarez – 2014-11-25T00:26:25.040

@FelipeAlvarez, My hands are not used to type < /some/file grep | grep | .... – Vi. – 2014-11-25T00:29:55.260

2What about grep /some/file – Felipe Alvarez – 2014-11-25T00:35:45.203

Thanks for this answer, it helped me on an embedded system that only had one flag for ps (-w for wide output) so all of the answers using ps did not work for me. Thanks! – Citizen Kepler – 2016-05-05T00:37:14.733

2grep '^PPid:' /proc/$1/status | grep -o '[0-9]*' is all you need. (It is very uncommon for Unix tools to do the if [ -z ]; then read thing.) – user1686 – 2010-06-08T11:12:14.737

@grawity It helps do do things like echo $$ | ppid | ppid | ppid – Vi. – 2010-06-09T13:04:00.907

4

On Linux:

ps hoppid $thatprocess

jthill

Posted 2010-06-08T09:42:29.587

Reputation: 141

2

$ 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

sebastian_t

Posted 2010-06-08T09:42:29.587

Reputation: 133

Useless use of echo? ;) – bobbogo – 2017-10-12T10:21:11.730

It is actually required on some terminals. To be honest I don't remember exactly but it actually solved a problem. :D – sebastian_t – 2017-10-13T07:08:05.247

1

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.

Praveen S.

Posted 2010-06-08T09:42:29.587

Reputation: 11

1It is to be used non-interactively. I already know that in htop you can configure PPID column. – Vi. – 2012-11-23T13:49:05.670

1

Here is a quick solution that should also work:

ps $$

Gevork Palyan

Posted 2010-06-08T09:42:29.587

Reputation: 79

That doesn't give the parent PID which is what the OP asked for. – Paused until further notice. – 2013-09-24T19:35:56.897

0

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 -p ${1:-$$} -h -o pid,ppid,args | \
    (
      read pid ppid args
      echo -e "$pid\t$args"
      [[ $pid -gt 1 ]] && main $ppid
    )
}

main "$@"

Bruno Bronosky

Posted 2010-06-08T09:42:29.587

Reputation: 1 251