Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Parent process

4 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Stu

ungelesen,
15.08.2005, 09:13:2315.08.05
an
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.

ungelesen,
15.08.2005, 09:34:5915.08.05
an

#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

ungelesen,
15.08.2005, 09:51:2115.08.05
an

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

ungelesen,
15.08.2005, 10:01:2915.08.05
an
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.

ungelesen,
15.08.2005, 10:22:2615.08.05
an
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

ungelesen,
15.08.2005, 11:01:0215.08.05
an

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

ungelesen,
16.08.2005, 00:45:1816.08.05
an

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

ungelesen,
16.08.2005, 03:04:4816.08.05
an
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

ungelesen,
16.08.2005, 03:49:3616.08.05
an

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

0 neue Nachrichten