Getting PID of background process in shell script.

Keith Gabryelski ag at amix.commodore.com
Thu Mar 29 07:49:20 AEST 1990


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

In article <3074 at auspex.auspex.com> 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 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 at amix.commodore.com        Keith Gabryelski          ...!cbmvax!amix!ag



More information about the Comp.lang.c mailing list