Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Dec 2012 21:08:37 +0000
From:      Steve O'Hara-Smith <ateve@sohara.org>
To:        freebsd-questions@freebsd.org
Subject:   Re: Safe Way to Tell if Process is Running
Message-ID:  <20121204210837.1eaa847ea1eaec7850ba8054@sohara.org>
In-Reply-To: <201212042050.qB4Koc70086364@x.it.okstate.edu>
References:  <201212042050.qB4Koc70086364@x.it.okstate.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 04 Dec 2012 14:50:38 -0600
Martin McCormick <martin@x.it.okstate.edu> wrote:

> Robert Bonomi writes:
> > 'man 2  kill' tells all.
> 
> 	I believe that is the first or second time I have used
> Section 2. I appreciate the reminder. It looks like ps -p ###
> >/dev/null appears to do what I need without producing output
> 
> ps -p 54321 >/dev/null && date ran the date command if there was
> a process with that number and produced nothing if no process
> 54321 existed.

	That's not a certain test, ps can miss processes. Given that you
are working in C you would be better off calling kill directly rather than
spawning a process with system and risking picking up some odd
implementation of a command.

	if (-1 != kill(pid, 0)) {
		// Process exists
	} else if (EPERM == errno) {
		// No permission to signal process - belongs to someone else
	} else if (ESRCH == errno) {
		// Process does not exist
	} else {
		// Something weird and undocumented went wrong
	}

-- 
Steve O'Hara-Smith <ateve@sohara.org>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121204210837.1eaa847ea1eaec7850ba8054>