[comp.unix.questions] How to get PID in backgrounded /bin/sh while loop?

montnaro@spyder.crd.ge.com (Skip Montanaro) (03/20/90)

Given the small shell script:

    #!/bin/sh

    ... do some stuff ...

    while true ; do

	... do some loopy stuff ...

    done &

how do you discover the process id of the backgrounded while loop process?

A colleague suggested I write a small C program that just calls getppid(2),
but that is overkill for the situation I have at hand. I've tried various
combinations of quoting $$, evaling things containing it, and so on, but
nothing yields helpful results. $! is undefined within the loop.



--
Skip (montanaro@crdgw1.ge.com)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/21/90)

In article <MONTNARO.90Mar20094531@spyder.crd.ge.com> montanaro@crdgw1.ge.com (Skip Montanaro) writes:
: 
: Given the small shell script:
: 
:     #!/bin/sh
: 
:     ... do some stuff ...
: 
:     while true ; do
: 
: 	... do some loopy stuff ...
: 
:     done &
: 
: how do you discover the process id of the backgrounded while loop process?
: 
: A colleague suggested I write a small C program that just calls getppid(2),
: but that is overkill for the situation I have at hand. I've tried various
: combinations of quoting $$, evaling things containing it, and so on, but
: nothing yields helpful results. $! is undefined within the loop.

How 'bout

	pid=`perl -e 'print getppid;'`

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

maart@cs.vu.nl (Maarten Litmaath) (03/21/90)

In article <MONTNARO.90Mar20094531@spyder.crd.ge.com>,
	montnaro@spyder.crd.ge.com (Skip Montanaro) writes:
)...
)    while true ; do
)
)	... do some loopy stuff ...
)
)    done &
)
)how do you discover the process id of the backgrounded while loop process?
)
)A colleague suggested I write a small C program that just calls getppid(2),
)...

You could place such a utility in, say, /usr/local/bin for the time being.
I don't know what POSIX has to say about it, but somehow the child's pid
ought to be easily determined.  Instead of

	mypid=`getppid`

...I'd rather see something like

	mypid=$%

Nowadays the only alternative to the C program is:

	sh -c '
		pid=$$

		while :
		do
			# some loopy stuff with $pid
		done
	' &
--
 1) Will 4.5BSD have wait5()?         |Maarten Litmaath @ VU Amsterdam:
 2) Sleep(3) should be sleep(2) again.|maart@cs.vu.nl, uunet!mcsun!botter!maart