[comp.unix.questions] How to tell if a process is active?

mhoffman@infocenter.UUCP (Mike Hoffman) (06/15/89)

I have an application in which I need to check to see if a process
is currently active. The only method I have been able to devise
is the "#" option of "ps", as in  "ps #1234" which will print out
a header, then the information on process 1234:

% ps #1234
  PID TT   STAT  TIME COMMAND
 1234 117  IW    0:00  foo bar baz

If process 1234 is not active, I get just the first line (the
column headers), and there is nothing else.

What I do, more or less, is: 

isactive(pid)
	open pipe
	fork
	if (child)
		redirect stdout to pipe
        	execl ("bin/ps","ps","#pid",0)
	else
		read(pipe,sizeof(header line))
		if (anything else to read)
			return(process is active)
		else
			return(process is not active)

This seems terribly clumsy, and I'm sure there is a better way :-(

It works, and seems dependable so far, but it seems to me that
if "ps #1234" can do it, then I should be able to do it as well.

Any help or suggestions will be appreciated. I hope this won't
generate as many answers as "what does g/re/p mean?", but send
answers e-mail, if possible - I'll summarize if volume warrants
it.

				m/ik/e
----
UUCP: {uunet,codas!novavax,sun,pur-ee}!gould!mhoffman  
                                  
Michael J. Hoffman                 "My opinions cannot be confused
Manufacturing Engineering           with those of my employer, as
Encore Computer Corporation         I don't even know what those
                                    opinions might be."

wescott@ncrcae.Columbia.NCR.COM (Mike Wescott) (06/15/89)

In article <2727@infocenter.UUCP> mhoffman@infocenter.UUCP (Mike Hoffman) writes:
> 
> I have an application in which I need to check to see if a process
> is currently active. 

Try:

isactive(pid)
	if(kill(pid,0)==0 || errno == EPERM)
		return(process is active)
	else 
		return(process is not active)

see kill(2).

-- 
	-Mike Wescott
	 mike.wescott@ncrcae.Columbia.NCR.COM

cowan@marob.masa.com (John Cowan) (06/17/89)

In article <4608@ncrcae.Columbia.NCR.COM> wescott@ncrcae.Columbia.NCR.COM (Mike Wescott) writes:
>In article <2727@infocenter.UUCP> mhoffman@infocenter.UUCP (Mike Hoffman) writes:
>> 
>> I have an application in which I need to check to see if a process
>> is currently active. 
>
>Try:
>
>isactive(pid)
>	if(kill(pid,0)==0 || errno == EPERM)
>		return(process is active)
>	else 
>		return(process is not active)
>
>see kill(2).
>
>-- 
>	-Mike Wescott
>	 mike.wescott@ncrcae.Columbia.NCR.COM


That doesn't work on all flavors of Unix.  Xenix supports it, so does
SVR2, but definitely not in V7.  I don't know about BSD.

Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
Fascist inews!
-- 
John Cowan <cowan@marob.masa.com> or <cowan@magpie.masa.com>
UUCP mailers:  ...!uunet!hombre!{marob,magpie}!cowan
Fidonet (last resort): 1:107/711
Aiya elenion ancalima!