Getting PID of background process in shell script.

Christopher H. Starr starr at tybalt.caltech.edu
Sat Mar 24 12:43:15 AEST 1990


hitz at auspex.auspex.com (Dave Hitz) writes:

>In a shell script I want to start a process in the background and then
>kill it at some later time.  To do this I want to save its pid in a
>variable.
> [... stuff deleted ...]
>Anyone have a [clean] way to do this?
>Dave Hitz					home: 408-739-7116
>UUCP: {uunet,mips,sun,bridge2}!auspex!hitz 	work: 408-492-0900

Why not use the shell variable '$!' which is defined as the process
number of the last process run in the background (in decimal)?  In
your script (which I assume is a Bourne shell script), you can have,
for example,
______________________
a.out &
PID=$!
: ' ... do stuff ... '
kill $PID
______________________
I hope this helps.
- Christopher Starr ( starr at tybalt.caltech.edu )



More information about the Comp.lang.c mailing list