| Stephane CHAZELAS 2007-05-24, 7:20 am |
| 2007-05-23, 21:52(+02), Michael Tosch:
> Janis Papanagnou wrote:
>
> or
>
> /bin/ps -o comm -p $PPID
>
> The OS does not keep track about terminated processes.
> But at the beginning of your script there is a good chance that
> the parent process is yet alive.
[...]
Note that it is not clear what $PPID contains, at least
according to POSIX. It says:
PPID
Set by the shell to the decimal process ID of the
process that invoked this shell. In a subshell (see
Shell Execution Environment), PPID shall be set to the
same value as that of the parent of the current shell.
For example, echo $ PPID and ( echo $ PPID ) would
produce the same value. This volume of IEEE Std
1003.1-2001 specifies the effects of the variable only
for systems supporting the User Portability Utilities
option.
That doesn't make sense to me. How would a process invoke
another process? The most sensible way would be via an exec*()
system call, in which case $$ would be the same as $PPID.
The definition for "invoke" is not very helpful:
To perform command search and execution actions, except
that searching for shell functions and special built-in
utilities is suppressed; see also Execute.
Practically, $PPID contains the value of getppid() at the time
of the shell's initialisation, so in cases where the parent is
gone by the time the shell does the getppid(), $PPID will likely
be 1, even though I don't think we can say that init invoked
that shell.
ps -p "$$" -o ppid=
will return the parent process id of the shell at the time this
command is run (which may be different from $PPID if $PPID is
gone by now).
~$ (sh -c 'ls -d /proc/$PPID' &); sleep 1
/proc/1
~$ (sh -c 'sleep 2; ls -d /proc/$PPID' & sleep 1); sleep 2
ls: /proc/8121: No such file or directory
--
Stéphane
|