From owner-freebsd-questions@FreeBSD.ORG Thu Jan 22 21:58:04 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 D4D49106566B for ; Thu, 22 Jan 2009 21:58:04 +0000 (UTC) (envelope-from mkhitrov@gmail.com) Received: from fk-out-0910.google.com (fk-out-0910.google.com [209.85.128.186]) by mx1.freebsd.org (Postfix) with ESMTP id 6070B8FC13 for ; Thu, 22 Jan 2009 21:58:04 +0000 (UTC) (envelope-from mkhitrov@gmail.com) Received: by fk-out-0910.google.com with SMTP id f40so989695fka.11 for ; Thu, 22 Jan 2009 13:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=mHk/jK/D9qP5Hy3Z0U/S634qIQ4866oNNy2RRJKBL+w=; b=Nd/QR4HkLITAnT4lXdwTa4+aj3urm99my/KfDHuv/R8UzWO7MCMxIZ9rn8C9zm2qLW dwUcO4f3KanhJitg7yj0Ax/FuwM+1X9wD83+flLMI63wvnjb60IIus9YrUPZxBSyhH/p YV2niAWKt2zdJpwbZqeSMhgFSWlYM2FEcmxF8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=pnRqYjlrhlad04dV64dIFxqFtAaRFyMhl3G60ZgUTWKy5mb3bgFLdkGnzg5Vjr2SHj oWUbqGrkl0YIB2mWmzuGmFXGD55GCnZPWprDQ3qf06OJsP8fzsnV/FyW3NHcEVc+y545 DTHizue6NLji4ROAgXiLd+62scw1CNXXejj5Y= MIME-Version: 1.0 Received: by 10.223.114.79 with SMTP id d15mr163323faq.88.1232660008221; Thu, 22 Jan 2009 13:33:28 -0800 (PST) In-Reply-To: <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> References: <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> Date: Thu, 22 Jan 2009 16:33:28 -0500 Message-ID: <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com> From: Maxim Khitrov To: Nerius Landys Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: 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 21:58:05 -0000 On Thu, Jan 22, 2009 at 3:41 PM, Nerius Landys wrote: > 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? Give this a try: #!/bin/sh java() { echo 'start' sleep 5 echo 'stop' } sleep 1 && kill $$ & java kill $! Replace the java function with your code. The basic idea is that you start a child process which will kill its parent after 1 second (parent pid is $$, child is $!). If the java command terminates before then, the child is killed. Test both cases out by commenting out 'sleep 5' line above. - Max