From owner-freebsd-questions@FreeBSD.ORG Thu Jan 22 20:41:16 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 10D121065670 for ; Thu, 22 Jan 2009 20:41:16 +0000 (UTC) (envelope-from nlandys@gmail.com) Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.159]) by mx1.freebsd.org (Postfix) with ESMTP id 965738FC12 for ; Thu, 22 Jan 2009 20:41:15 +0000 (UTC) (envelope-from nlandys@gmail.com) Received: by fg-out-1718.google.com with SMTP id l26so2768882fgb.35 for ; Thu, 22 Jan 2009 12:41:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type:content-transfer-encoding; bh=xNZT9sK8cxvomocA6PPvweqkrjDbt686Ppb3iWl0Bmw=; b=ryMl4BdFmpmr8uKLaTw5YNfOBdI4a0rlMt3ygijTMQ/UgKIkDVuhW+yXBoWnrSzw7M G3din04OEpkz0svMIGF0+2SAcaBPyJ+AqiffQ5r+QuOhQpJ6n1jI2hpNiRXSmXg8AqRp 98Nb0dzJ4xLP0VrLBl4OpZcwr8FnE43XuySt4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=QXD4F4fKkRlHts3JnVJL9ppJ3x8F4ihBFr4T6HBg6Pu1KaT82A0wKPMLnOIghR73jE BNq/pm87J+GoWTMhYtBL0psb4+ym7gsYkdaTGSjb+rrOabc59AcGwBrals94VQMknna/ Yt1/9xHBVjhqGVY2zplc5KPSCEkGfsaIlbMlo= MIME-Version: 1.0 Received: by 10.181.33.18 with SMTP id l18mr798960bkj.192.1232656874403; Thu, 22 Jan 2009 12:41:14 -0800 (PST) Date: Thu, 22 Jan 2009 12:41:14 -0800 Message-ID: <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> From: Nerius Landys To: freebsd-questions@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: shell scripting, how to auto-timeout? 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: Thu, 22 Jan 2009 20:41:16 -0000 This is a shell scripting question, it is not specific to FreeBSD. I am writing a script that I want to terminate after 1 second (because it has the potential to infinite loop). The script I have so far is: #!/bin/sh cd `dirname "$0"` CLASSPATH=mapgen.jar export CLASSPATH /usr/local/bin/java PipeGenerator $* The java process has the potential to run forever, and I want it to run for at most 1 second then get killed. I could write a parent script that somehow gets the PID of the child script, but the problem is that the java program writes to standard out, the result of the program is written to standard out. I also don't really want to share a PID with a temporary file. So what I might do is this: /usr/local/bin/java PipeGenerator $* & sleep 1 Also with the above code I would be waiting for 1 second even if the java process finished sooner. But that is a penalty I'm willing to pay, unless there is a more elegant solution. How do I do this?