Path: utzoo!utgpu!water!watmath!clyde!burl!codas!killer!fmsrl7!swlabs!csd_v!bak
From: bak@csd_v.UUCP (Bruce)
Newsgroups: comp.unix.wizards
Subject: Re: Is process  alive?
Summary: try reading pipe output of ps -ef
Message-ID: <141@csd_v.UUCP>
Date: 11 Dec 87 01:50:26 GMT
References: <1454@rtech.UUCP> <1921@munnari.oz> <429@minya.UUCP>
Organization: Computer Systems Design -- Sandy Hook, Ct.
Lines: 45

In article <429@minya.UUCP>, jc@minya.UUCP (John Chambers) writes:
> Is there a universal way that will work on any Unix to write a function
> 	isprocess(n)
> which returns TRUE if process n is alive, and FALSE if it isn't alive?
> 
> Note that I have said nothing about the relationship of process n (if it
> exists) to the process that is asking.  They might or might not be related.
> They might or might not have the same uid and gid.  I don't want to kill
> the process (or even upset it in any way).  I just want to know if it is
> alive.
> 

This solution is a little awkward but will definitely work.

Open a read-only pipe to the "ps -ef" command ( "-al" on BSD I believe, anyway
there are your #ifdefs ) and read the output searching the PID field for the
process of interest to you.  

I have a routine (from Kernighan and Pike) called zap which matches any string
in the line and then does a kill -9 to the pid, thereby allowing me to kill
processes mneumonically rather than PID.  Thus

	$ tail -f abc &
	9803
	.
	.
	many screenfulls of junk later
	.
	$ zap abc
     bak  1632  1354  0 19:51:13 cons2    0:58 tail -f ggg ? n   
     bak  1648  1354  0 19:51:13 cons2    0:58 tail -f abc ? y
	$
	ksh: 1648 Killed


The same idea is applicable to just looking at the PID field for your case.
Of course ps is not the quickest command to execute and if there are a lot
of processes running quite a bit of winnowing has to be done.  But if no
better solutions come along, this will do the job and is highly portable.
One other thing it will absolutely be transparent ot the running process
on all flavors of unix.
-- 
Bruce Kern                                      | uunet!swlabs!csd_v!bak
Computer Systems Design                         | 1-203-270-0399
29 High Rock Rd., Sandy Hook, Ct. 06482         | This space for rent.