zsh-users
 help / color / mirror / code / Atom feed
From: Chris Johnson <johnch@uwec.edu>
To: zsh-users@zsh.org
Subject: The Halting Problem
Date: Sat, 28 Dec 2013 10:49:38 -0600	[thread overview]
Message-ID: <20131228164937.GA44192@cs2666372x.uwec.edu> (raw)

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 <<EOF
  public class Foo {
    public static void main(String[] args) {
      for (int i = 0; true; i = (i + 1) % 4) {
        System.out.print("\r" + "\\\\|/-".charAt(i));
      }
    }
  }
  EOF
  ) > 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


             reply	other threads:[~2013-12-28 17:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-28 16:49 Chris Johnson [this message]
2013-12-28 20:43 ` Bart Schaefer
2013-12-29 13:35   ` Chris Johnson
2013-12-30  0:53 ` Mikael Magnusson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20131228164937.GA44192@cs2666372x.uwec.edu \
    --to=johnch@uwec.edu \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).