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.
#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
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.
cat junk
x.sh PID=27108
y.sh PARENT= PID=27110
Any other ideas?
>> #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.
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.
I use:
ppid=`ps -o ppid -p $$ | sed 1d | tr -d " "`
The sed 1d removes the header line produced by ps.
Bill Seivert
How about:
ppid=`ps -o ppid= -p $$`
Or even
ppid=`ps --no-headers -o ppid -p $$`
John
--
use Perl;
program
fulfillment
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}"