Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
3  
I've always found typing out my question on stackoverflow/superuser/serverfault helps me realize the solution without help. I guess it's an extension of Rubber Ducking en.wikipedia.org/wiki/Rubber_duck_debugging –  Puddingfox Jun 8 '10 at 14:09
1  
@Puddingfox I was recommended to keep adding information (if not duplicate) even if I found out answer myself. –  Vi. Jun 9 '10 at 13:09

6 Answers 6

up vote 28 down vote accepted

Command line:

ps -p 1111 -o ppid=

Function:

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

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.

share|improve this answer
    
The = sign is not necessary, at least on OS X 10.8.2. –  jtbandes Jan 9 '13 at 19:04
2  
@jtbandes: The equal sign as used here suppresses the output of the header line (Linux and OS X). –  Dennis Williamson Jan 9 '13 at 21:08
    
Aha, so it does! Thanks! –  jtbandes Jan 9 '13 at 21:13

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
share|improve this answer

Parent pid is in shell variable PPID, so

echo $PPID

share|improve this answer
    
Yes, but 1. I want parent pid of other process, 2. I want to be able to traverse all ancestors to init. –  Vi. Sep 24 '12 at 12:37
    
On the other hand, using $PPID did just solve the problem I had which Google suggested this page as an answer to. –  Paul Whittaker Sep 24 '12 at 15:58

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|improve this answer
    
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.) –  grawity Jun 8 '10 at 11:12
    
@grawity It helps do do things like echo $$ | ppid | ppid | ppid –  Vi. Jun 9 '10 at 13:04

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.

share|improve this answer
1  
It is to be used non-interactively. I already know that in htop you can configure PPID column. –  Vi. Nov 23 '12 at 13:49

Here is a quick solution that should also work:

ps $$
share|improve this answer
    
That doesn't give the parent PID which is what the OP asked for. –  Dennis Williamson Sep 24 '13 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.