From owner-freebsd-questions@FreeBSD.ORG Fri Jan 23 18:58:12 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 8E7A91065674 for ; Fri, 23 Jan 2009 18:58:12 +0000 (UTC) (envelope-from nlandys@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 19B548FC14 for ; Fri, 23 Jan 2009 18:58:11 +0000 (UTC) (envelope-from nlandys@gmail.com) Received: by fk-out-0910.google.com with SMTP id f40so1481585fka.11 for ; Fri, 23 Jan 2009 10:58:11 -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=HGS7OwGc2HEyjuYswGozOZKn3yKfM4EB0dKA5cEWgDg=; b=isOYJr/b/QFB82MpyvqvymkhF2ug6xZhLXMdrRIqEbZ//pvn0YAsqm8SjdhCWjLf33 tSZfm155Z/hoWxqsW6xKVFM2EceMJJiLMHgRyCev8b9Mg+y3NI+oxlWp5W/LnNbsDfzz g+yGhVh8A++4NuW0TJBd8kCYDAsByzTPlBe3A= 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=VUyEz+SHmPnK3NBrFaskYjQ9dZA2VjQ99n6LNvd461jhGjRag3nZLMtza/rFO5eCsq N/RAvQJ7HaDP9XtkVLkv+xUtVE4UdeHCeSgkRp5bPmD1nLzvKE10QlP5/eYE2+/KuYI0 7WHjZHc72Ytwdf1SYDl40Z7oovbjNSiDv7Law= MIME-Version: 1.0 Received: by 10.181.48.4 with SMTP id a4mr544467bkk.6.1232737091066; Fri, 23 Jan 2009 10:58:11 -0800 (PST) In-Reply-To: <26ddd1750901221635k17230c7eudb1edc38c808eb83@mail.gmail.com> References: <560f92640901221241y4fc1620aree083a812c1f3c8d@mail.gmail.com> <26ddd1750901221333x5356f4f3l6b6410fc05d4e6d4@mail.gmail.com> <560f92640901221451j2e2b259bw1559a8c8d8912941@mail.gmail.com> <560f92640901221458y9409360n34904461fb2580e4@mail.gmail.com> <26ddd1750901221635k17230c7eudb1edc38c808eb83@mail.gmail.com> Date: Fri, 23 Jan 2009 10:58:11 -0800 Message-ID: <560f92640901231058i3c9e8645n6a30c74ec868ba87@mail.gmail.com> From: Nerius Landys To: Maxim Khitrov Content-Type: text/plain; charset=ISO-8859-1 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: Fri, 23 Jan 2009 18:58:12 -0000 > #!/bin/sh -T > > kill_all() > { > echo 'killing everything' > kill $SPID $CPID 2> /dev/null > exit 0 > } > > trap kill_all SIGCHLD > > ./child & > CPID=$! > > sleep 5 & > SPID=$! > > echo "child is $CPID" > echo "sleeper is $SPID" > wait This is very nice. However I'm getting one problem still. My script prints a "Terminated" to standard out, and this is bad because the purpose of this java program is to print to standard out, so the output gets jumbled. The script I have is pasted below, and the "Terminated" string seems to be printed out from the kill command that kills the sleep thread. #!/bin/sh -T cd `dirname "$0"` CLASSPATH="mapgen.jar" export CLASSPATH kill_all() { kill "$JAVA_PID" > /dev/null 2>&1 JAVA_KILL_EXIT_STATUS="$?" EXIT_STATUS=0 if [ "$JAVA_KILL_EXIT_STATUS" -eq 0 ]; then echo "Terminated infinite looping in Java process." 1>&2 EXIT_STATUS=1 fi kill "$SLEEP_PID" > /dev/null 2>&1 exit "$EXIT_STATUS" } trap kill_all SIGCHLD /usr/local/bin/java PipeGenerator $* & JAVA_PID="$!" sleep 3 & SLEEP_PID="$!" wait