* The Halting Problem
@ 2013-12-28 16:49 Chris Johnson
2013-12-28 20:43 ` Bart Schaefer
2013-12-30 0:53 ` Mikael Magnusson
0 siblings, 2 replies; 4+ messages in thread
From: Chris Johnson @ 2013-12-28 16:49 UTC (permalink / raw)
To: zsh-users
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: The Halting Problem
2013-12-28 16:49 The Halting Problem Chris Johnson
@ 2013-12-28 20:43 ` Bart Schaefer
2013-12-29 13:35 ` Chris Johnson
2013-12-30 0:53 ` Mikael Magnusson
1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2013-12-28 20:43 UTC (permalink / raw)
To: zsh-users
On Dec 28, 10:49am, Chris Johnson wrote:
}
} 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.
You're almost there. If ctrl+c won't kill the java process in the above
example, then there's some additional signal handling going on behind
the scenes, and you just need to add a trap before the "wait":
TRAPINT() { kill $longpid }
So that when wait is interrupted you send off a signal to java. You
probably also want to kill $sleeppid there, but because it's a shell
function it may ignore the default TERM signal, so:
TRAPINT() {
kill $longpid
kill -HUP -$$
}
That should clean everything up.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: The Halting Problem
2013-12-28 20:43 ` Bart Schaefer
@ 2013-12-29 13:35 ` Chris Johnson
0 siblings, 0 replies; 4+ messages in thread
From: Chris Johnson @ 2013-12-29 13:35 UTC (permalink / raw)
To: zsh-users
Bart Schaefer sent me the following 1.0K:
> } This kills the long-running job on timeout, but it also puts the job
> } in the background. Control-C won't kill it.
>
> You're almost there. If ctrl+c won't kill the java process in the above
> example, then there's some additional signal handling going on behind
> the scenes, and you just need to add a trap before the "wait"...
Bart, this is great. Thanks!
I've generalized the script so that it takes an arbitrary long-running command through ARGV (like time) and kills it after the specified number of seconds:
#!/usr/bin/env zsh
if [[ $# -lt 2 ]]; then
echo "Usage: $0 nseconds command [arg1 [arg2 ...]]" >&2
exit 1
fi
nseconds=$1
shift
# The command to run is in ARGV[2..]. cmd is going to be embedded in a
# string, so we'll need to do some quoting of # its elements to ensure
# correct interpolation.
cmd=(${(q-)@})
# Kills the specified process after nseconds have expired.
sleepkill() {
sleep $1
kill $2
print "Command $cmd timed out."
}
# Start up both the long-running process and a sleep timer. The parentheses
# are needed to background the entire cmd, not just its last subcommand.
eval "($cmd) &"
longpid=$!
sleepkill $nseconds $longpid &
sleeppid=$!
# By default, Control-C will kill the wait that happens below -- and not
# longpid. I want it to kill longpid and the sleep timer.
TRAPINT() {
kill $longpid
kill -HUP -$$
}
# If longpid has already finished, wait will flash a message saying it doesn't
# know about the process. I don't want to see that message.
wait $longpid 2>/dev/null
# If longpid finishes before the sleep timer, let's kill the sleep timer.
kill $sleeppid 2>/dev/null
If you see any improvements that can be made, I welcome input.
--
Chris Johnson
johnch@uwec.edu
http://www.cs.uwec.edu/~johnch
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: The Halting Problem
2013-12-28 16:49 The Halting Problem Chris Johnson
2013-12-28 20:43 ` Bart Schaefer
@ 2013-12-30 0:53 ` Mikael Magnusson
1 sibling, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2013-12-30 0:53 UTC (permalink / raw)
To: Chris Johnson; +Cc: Zsh Users
On 28 December 2013 17:49, Chris Johnson <johnch@uwec.edu> wrote:
> 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'm thankful for any suggestions!
If you happen to have a fairly new version of gnu coreutils installed,
the 'timeout' command might be of interest here.
--
Mikael Magnusson
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-12-30 0:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-28 16:49 The Halting Problem Chris Johnson
2013-12-28 20:43 ` Bart Schaefer
2013-12-29 13:35 ` Chris Johnson
2013-12-30 0:53 ` Mikael Magnusson
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).