From owner-freebsd-questions@FreeBSD.ORG Mon Dec 12 17:12:05 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 24E6E16A41F for ; Mon, 12 Dec 2005 17:12:05 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from rosebud.otenet.gr (rosebud.otenet.gr [195.170.0.94]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2295E43D6D for ; Mon, 12 Dec 2005 17:11:54 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from flame.pc (aris.bedc.ondsl.gr [62.103.39.226]) by rosebud.otenet.gr (8.13.4/8.13.4/Debian-8) with SMTP id jBCHBkGh029090; Mon, 12 Dec 2005 19:11:47 +0200 Received: by flame.pc (Postfix, from userid 1001) id ED7F711459; Mon, 12 Dec 2005 19:10:56 +0200 (EET) Date: Mon, 12 Dec 2005 19:10:56 +0200 From: Giorgos Keramidas To: Eric Schuele Message-ID: <20051212171056.GA4879@flame.pc> References: <439D9FA3.7030603@computer.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <439D9FA3.7030603@computer.org> Cc: freebsd-questions@freebsd.org Subject: Re: grep'ping the ps output.... X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Dec 2005 17:12:05 -0000 On 2005-12-12 10:04, Eric Schuele wrote: > Hello, > > I am sure this is quite trivial, but... > > I have need to determine if an app (firefox, or anything really) is > already running before I perform some action. So I grep the ps output. > However sometimes (many times) that which I'm searching for is present > in the output because I am presently grep'ing for it. But it is not > always there. It seems to be a bit inconsistent. (See below). > > I was wondering if someone could explain why it is sometimes there and > not other times. And how I should correctly go about detecting if the > process is running before I perform my action. pgrep(1) is nice for this sort of thing :) > %ps | grep firefox^M^M > 703 v0 I 0:00.00 /bin/sh /usr/X11R6/bin/firefox^M > 722 v0 I 0:00.00 /bin/sh /usr/X11R6/lib/firefox/run-mozilla.sh > /usr/X1^ > M > 734 v0 S 0:10.92 /usr/X11R6/lib/firefox/firefox-bin^M > %ps | grep firefox^M^M > 703 v0 I 0:00.00 /bin/sh /usr/X11R6/bin/firefox^M > 722 v0 I 0:00.00 /bin/sh /usr/X11R6/lib/firefox/run-mozilla.sh > /usr/X1^ A similar thing with pgrep(1) works as expected: % flame:/home/keramida$ for count in 1 2 3 4 5 ; do % > pgrep firefox ; echo ; sleep 1 ; done % 1470 % % 1470 % % 1470 % % 1470 % % 1470 % % flame:/home/keramida$ for count in 1 2 3 4 5 ; do % > pgrep -l firefox ; echo ; sleep 1 ; done % 1470 firefox-bin % % 1470 firefox-bin % % 1470 firefox-bin % % 1470 firefox-bin % % 1470 firefox-bin % % flame:/home/keramida$