From owner-freebsd-questions@FreeBSD.ORG Tue Dec 4 21:08:49 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 6F72EF36 for ; Tue, 4 Dec 2012 21:08:49 +0000 (UTC) (envelope-from ateve@sohara.org) Received: from uk1rly2283.eechost.net (relay01a.mail.uk1.eechost.net [217.69.40.75]) by mx1.freebsd.org (Postfix) with ESMTP id 2AE638FC15 for ; Tue, 4 Dec 2012 21:08:48 +0000 (UTC) Received: from [31.186.37.179] (helo=smtp.marelmo.com) by uk1rly2283.eechost.net with esmtpa (Exim 4.72) (envelope-from ) id 1Tfzk3-0006gH-St for freebsd-questions@freebsd.org; Tue, 04 Dec 2012 21:09:23 +0000 Received: from [192.168.63.1] (helo=steve.marelmo.com) by smtp.marelmo.com with smtp (Exim 4.80.1 (FreeBSD)) (envelope-from ) id 1TfzjL-000Aqj-Q1 for freebsd-questions@freebsd.org; Tue, 04 Dec 2012 21:08:39 +0000 Date: Tue, 4 Dec 2012 21:08:37 +0000 From: Steve O'Hara-Smith 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> X-Mailer: Sylpheed 3.2.0 (GTK+ 2.24.6; amd64-portbld-freebsd9.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Auth-Info: 15567@permanet.ie (plain) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Dec 2012 21:08:49 -0000 On Tue, 04 Dec 2012 14:50:38 -0600 Martin McCormick 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