[comp.lang.c] Getting PID of background process in shell script.

starr@tybalt.caltech.edu (Christopher H. Starr) (03/24/90)

hitz@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@tybalt.caltech.edu )

rdg@virtech.uucp (Roger D. Gough) (03/25/90)

In article <3074@auspex.auspex.com> hitz@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 it's pid in a
>variable.
>
The builtin shell variable $! contains the process number of the last process
run in the background.  So you want to do something like:

		run a command &
		PID=$!
		# do some stuff
		kill $PID
-- 
Roger D. Gough                                                  +1 703 689 1692
Sequel Technologies                                            uunet!sequel!rdg

donlash@uncle.UUCP (Donald Lashomb) (03/25/90)

In article <3074@auspex.auspex.com= hitz@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 it's pid in a
=variable.
=
=I can get the pid into a variable like this
=
=	PID=` exec 2>&- sh -ic '( sleep 1000 0>&- 2>&- & ) 2>&1' `
=
=but this seems kind of ugly.


what about PID=$!    - or am I misunderstanding something here.

-Don

peter@ficc.uu.net (Peter da Silva) (03/25/90)

How about redirecting this to a more appropriate newsgroup, like
comp.unix.questions...?
-- 
 _--_|\  `-_-' Peter da Silva. +1 713 274 5180. <peter@ficc.uu.net>.
/      \  'U`
\_.--._/
      v

ag@amix.commodore.com (Keith Gabryelski) (03/29/90)

[Article cut down c.u.w, c.u.q, and c.l.c AND followups directed to
 comp.unix.questions]

In article <3074@auspex.auspex.com> hitz@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 it's pid in a
>variable.

From your example (not shown) you seem to be using the bourne shell
which has the ``$!'' notation for the last background process
executed.

So a shell script of the form:

	sleep 10000 &			# Through in a background process.
	ps				# Ps will show us what is running.
	echo Sleeps PID is $!
	sleep 3
	echo Killing PID $!
	kill $!				# Kill the backgrounf process.
	ps				# We should see the sleep anymore.

Should be an example of what you are trying to do.

Pax, Keith
-- 
ag@amix.commodore.com        Keith Gabryelski          ...!cbmvax!amix!ag