Parent process

4 views
Skip to first unread message

Stu

unread,
Aug 15, 2005, 9:13:23 AM8/15/05
to
Is there a denotation in the shell to get the parent process. I know
that $$ is current PID.

For example,

cat x.sh
=========
rm -rf /tmp/junk
echo "x.sh PID=$$" > /tmp/junk

sh "y.sh"


cat y.sh
========
echo "y.sh PARENT PID= PID=$$" >> /tmp/junk

>./x.sh

In y.sh I want the PID of x.sh.

Thanks in advacne for all that answer this post.

Stachu 'Dozzie' K.

unread,
Aug 15, 2005, 9:34:59 AM8/15/05
to

#v+
[dozzie@hans shell]$ man bash | col -b | grep -i '\<parent\>' | head -n 1
PPID The process ID of the shell's parent. This vari­
[dozzie@hans shell]$
#v-

SUSv3 guarantees this variable:
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_03
http://tinyurl.com/a2vta (the same shorter)

--
Feel free to correct my English
Stanislaw Klekot

Icarus Sparry

unread,
Aug 15, 2005, 9:51:21 AM8/15/05
to

If you can edit 'x.sh', then the most portable thing is to change the

sh "y.sh"

to

XPID=$$ sh "y.sh"

and then use $XPID inside y.sh. Bash and ksh have a PPID variable, which
you can use if you have them, and can change the line to

ksh "y.sh"

If you can not change x.sh, and you can not change the 'sh' executable,
then you probably are going to have to parse the output of 'ps' or
something simular.

Stu

unread,
Aug 15, 2005, 10:01:29 AM8/15/05
to
When I add $PPID in y.sh nothing appears to print out. See output
below. Note I am running on Solaris 2.8 KSH


cat junk
x.sh PID=27108
y.sh PARENT= PID=27110


Any other ideas?

Stachu 'Dozzie' K.

unread,
Aug 15, 2005, 10:22:26 AM8/15/05
to
On 15.08.2005, Stu <beefs...@hotmail.com> wrote:
> Stachu 'Dozzie' K. wrote:
>> On 15.08.2005, Stu <beefs...@hotmail.com> wrote:
>> > Is there a denotation in the shell to get the parent process. I know
>> > that $$ is current PID.
>> >
>> > For example,
>> >
>> > cat x.sh
>> >=========
>> > rm -rf /tmp/junk
>> > echo "x.sh PID=$$" > /tmp/junk
>> >
>> > sh "y.sh"
>> >
>> >
>> > cat y.sh
>> >========
>> > echo "y.sh PARENT PID= PID=$$" >> /tmp/junk

>> #v+


>> [dozzie@hans shell]$ man bash | col -b | grep -i '\<parent\>' | head -n 1
>> PPID The process ID of the shell's parent. This vari­
>> [dozzie@hans shell]$
>> #v-
>>
>> SUSv3 guarantees this variable:
>> http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_05_03
>> http://tinyurl.com/a2vta (the same shorter)

> When I add $PPID in y.sh nothing appears to print out. See output


> below. Note I am running on Solaris 2.8 KSH
>
>
> cat junk
> x.sh PID=27108
> y.sh PARENT= PID=27110

That's funny. I have no way to check that in practice, but Solaris' ksh
manual (http://docs.sun.com/app/docs/doc/806-0624/6j9vek58g?a=view,
section "DESCRIPTION", subsection "Parameters Set by Shell") says that
ksh sets $PPID variable.

By the way, please don't toppost, because it's more difficult to read.

Netocrat

unread,
Aug 15, 2005, 11:01:02 AM8/15/05
to

To elaborate on the ps parsing, this works under sh on Solaris:

P_PID=`ps -p $$ -o ppid`
P_PID=`expr "$P_PID" : '[^0-9]*\([0-9]*\)'`
echo "Parent PID: $P_PID"

As an aside, recently I had to traverse up a process hierarchy in a shell
script. The essence of the code is this:

#! /usr/bin/sh

P_PID=$$
while test $P_PID -ne 1
do
P_PID=`ps -p $P_PID -o ppid` &&
P_PID=`expr "$P_PID" : '[^0-9]*\([0-9]*\)'` ||
break # avoid inf loop on errors
echo $P_PID
done

It is portable to any sh-derived shell; tested using sh under
net/open/freebsd, linux, solaris. The only doubt I have is
whether the -p and -o options as used here are universally recognised by
ps.

--
http://members.dodo.com.au/~netocrat

Bill Seivert

unread,
Aug 16, 2005, 12:45:18 AM8/16/05
to

I use:
ppid=`ps -o ppid -p $$ | sed 1d | tr -d " "`

The sed 1d removes the header line produced by ps.

Bill Seivert

John W. Krahn

unread,
Aug 16, 2005, 3:04:48 AM8/16/05
to
Bill Seivert wrote:
>
> Stu wrote:
>> Is there a denotation in the shell to get the parent process. I know
>> that $$ is current PID.
>>
>> For example,
>>
>> cat x.sh
>> =========
>> rm -rf /tmp/junk
>> echo "x.sh PID=$$" > /tmp/junk
>>
>> sh "y.sh"
>>
>>
>> cat y.sh
>> ========
>> echo "y.sh PARENT PID= PID=$$" >> /tmp/junk
>
> I use:
> ppid=`ps -o ppid -p $$ | sed 1d | tr -d " "`
>
> The sed 1d removes the header line produced by ps.

How about:

ppid=`ps -o ppid= -p $$`

Or even

ppid=`ps --no-headers -o ppid -p $$`

John
--
use Perl;
program
fulfillment

Netocrat

unread,
Aug 16, 2005, 3:49:36 AM8/16/05
to

Two alternatives to avoid the external calls to sed and tr; they
require a shell with variable substitution support:

1) this works under bash/zsh; under ksh the ! must be backslash-escaped or
replaced with a ^; under bash the ! may also be replaced with a ^:

ppid=`ps -o ppid -p $$` &&
ppid=${ppid//*[!0-9]}

2) this works on bash/ksh/zsh but assumes that the header ends in "PPID".
The let removes the newline and whitespace; under zsh the double quotes
are redundant:

ppid=`ps -o ppid -p $$` &&
let ppid="${ppid#*PPID}"

--
http://members.dodo.com.au/~netocrat

Reply all
Reply to author
Forward
0 new messages