From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29168 invoked by alias); 28 Dec 2013 17:07:32 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18259 Received: (qmail 4933 invoked from network); 28 Dec 2013 17:07:27 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.2 Date: Sat, 28 Dec 2013 10:49:38 -0600 From: Chris Johnson To: zsh-users@zsh.org Subject: The Halting Problem Message-ID: <20131228164937.GA44192@cs2666372x.uwec.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-OriginalArrivalTime: 28 Dec 2013 16:49:13.0854 (UTC) FILETIME=[BD3499E0:01CF03EC] Hi, folks. I have the following interest: I want to run a potentially long-running job interactively from a script, but if this job exceeds a certain duration, I want to kill it and notify the user. I've read up on traps and the NOTIFY option in From Bash to Z Shell, but I'm stuck. My current non-working proof-of-concept is: setopt LOCAL_OPTIONS setopt NOTIFY sleepkill() { sleep 5 print "timed out" kill $$ } sleepkill & # Create a consuming task. Let's have Java draw a spinner. (cat < Foo.java javac Foo.java # Here's the long running job. I want it in the foreground so I can # kill it manually, observe its output, etc. But if it's taking to # long, I want it automatically killed. java Foo When "sleep 5" finishes, the script itself is killed and the JVM process becomes an orphan. Is there a way I can kill the orphan too? I tried backgrounding the long-running job, capturing its PID, and passing that to sleepkill as the process to kill: java Foo & longpid=$! sleepkill $longpid & sleeppid=$! wait $longpid # Kill the sleep timer, if necessary. kill $sleeppid 2>/dev/null This kills the long-running job on timeout, but it also puts the job in the background. Control-C won't kill it. I'm thankful for any suggestions! -- Chris Johnson johnch@uwec.edu