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

256,739

Solution 1

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.

Solution 2

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 $$.

Solution 3

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

Solution 4

Parent pid is in shell variable PPID, so

echo $PPID

Solution 5

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]*"
Share:
256,739

Related videos on Youtube

Vi.
Author by

Vi.

Updated on September 17, 2022

Comments

  • Vi.
    Vi. over 1 year

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

    • Aquarius Power
      Aquarius Power over 9 years
      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?
    • Mancika
      Mancika about 8 years
      @AquariusPower Since you ask, fgrep is faster than grep. fgrep PPid status |cut -f2
    • Marian
      Marian about 7 years
      sed is way faster than grep and cut: sed -rn '/PPid/ s/^.*:\s+// p' < status
    • P....
      P.... almost 3 years
      pid=3773234; while true; do pid=$(awk '/^PPid:/{print $NF}' /proc/$pid/status);printf "$pid\n"; if [ $pid -eq 1 ];then break;fi;done|tac
  • user1686
    user1686 almost 14 years
    grep '^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.)
  • Vi.
    Vi. almost 14 years
    @grawity It helps do do things like echo $$ | ppid | ppid | ppid
  • Vi.
    Vi. over 11 years
    Yes, but 1. I want parent pid of other process, 2. I want to be able to traverse all ancestors to init.
  • Paul Whittaker
    Paul Whittaker over 11 years
    On the other hand, using $PPID did just solve the problem I had which Google suggested this page as an answer to.
  • Vi.
    Vi. over 11 years
    It is to be used non-interactively. I already know that in htop you can configure PPID column.
  • Assembler
    Assembler over 11 years
    The = sign is not necessary, at least on OS X 10.8.2.
  • Dennis Williamson
    Dennis Williamson over 11 years
    @jtbandes: The equal sign as used here suppresses the output of the header line (Linux and OS X).
  • Dennis Williamson
    Dennis Williamson over 10 years
    That doesn't give the parent PID which is what the OP asked for.
  • Sorceri
    Sorceri over 9 years
    UUOC useless use of cat
  • Vi.
    Vi. over 9 years
    @FelipeAlvarez, My hands are not used to type < /some/file grep | grep | ....
  • Sorceri
    Sorceri over 9 years
    What about grep /some/file
  • Citizen Kepler
    Citizen Kepler almost 8 years
    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!
  • sudo
    sudo over 6 years
    pstree is the nicest one I've seen here.
  • bobbogo
    bobbogo over 6 years
    Useless use of echo? ;)
  • sebastian_t
    sebastian_t over 6 years
    It is actually required on some terminals. To be honest I don't remember exactly but it actually solved a problem. :D
  • Connor McCormick
    Connor McCormick almost 5 years
    ps j is great because it's available on many distros and is easily composable
  • Alex78191
    Alex78191 about 4 years
    What about ps f?
  • nyov
    nyov almost 4 years
    @Alex78191 why do you ask? What about it? It does something completely different to what the question asked.
  • smac89
    smac89 over 3 years
    prefer alias if you want any shell auto-complete to still work
  • Bruno Bronosky
    Bruno Bronosky over 2 years
    @John-Karahalis I appreciate your edit. It was rejected by 2 other reviewers, but I agree and usually use long options to save readers time having to look up the meaning of cryptic flags. Thanks!
  • Admin
    Admin almost 2 years
    Auto-completion of function names works just fine for me.