How to get parent PID of a given process in GNU/Linux from command line?
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]*"
Related videos on Youtube
Vi.
Updated on September 17, 2022Comments
-
Vi. over 1 year
Resolved before asked:
cat /proc/1111/status | grep PPid
-
Aquarius Power over 9 yearsfaster:
grep PPid status |cut -f2
like intime(for((i=0;i<1000;i++));do grep PPid status |cut -f2 >/dev/null;done)
; wonder if there is something even faster? -
Mancika about 8 years@AquariusPower Since you ask, fgrep is faster than grep.
fgrep PPid status |cut -f2
-
Marian about 7 yearssed is way faster than grep and cut:
sed -rn '/PPid/ s/^.*:\s+// p' < status
-
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 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 theif [ -z ]; then read
thing.) -
Vi. almost 14 years@grawity It helps do do things like
echo $$ | ppid | ppid | ppid
-
Vi. over 11 yearsYes, but 1. I want parent pid of other process, 2. I want to be able to traverse all ancestors to init.
-
Paul Whittaker over 11 yearsOn the other hand, using
$PPID
did just solve the problem I had which Google suggested this page as an answer to. -
Vi. over 11 yearsIt is to be used non-interactively. I already know that in
htop
you can configurePPID
column. -
Assembler over 11 yearsThe
=
sign is not necessary, at least on OS X 10.8.2. -
Dennis Williamson over 11 years
-
Dennis Williamson over 10 yearsThat doesn't give the parent PID which is what the OP asked for.
-
Sorceri over 9 yearsUUOC useless use of cat
-
Vi. over 9 years@FelipeAlvarez, My hands are not used to type
< /some/file grep | grep | ...
. -
Sorceri over 9 yearsWhat about
grep /some/file
-
Citizen Kepler almost 8 yearsThanks 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 over 6 years
pstree
is the nicest one I've seen here. -
bobbogo over 6 yearsUseless use of echo? ;)
-
sebastian_t over 6 yearsIt is actually required on some terminals. To be honest I don't remember exactly but it actually solved a problem. :D
-
Connor McCormick almost 5 years
ps j
is great because it's available on many distros and is easily composable -
Alex78191 about 4 yearsWhat about
ps f
? -
nyov almost 4 years@Alex78191 why do you ask? What about it? It does something completely different to what the question asked.
-
smac89 over 3 yearsprefer alias if you want any shell auto-complete to still work
-
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 almost 2 yearsAuto-completion of function names works just fine for me.