From: Chris Johnson <johnch@uwec.edu>
To: zsh-users@zsh.org
Subject: Re: The Halting Problem
Date: Sun, 29 Dec 2013 07:35:55 -0600 [thread overview]
Message-ID: <20131229133554.GA46017@cs2666372x> (raw)
In-Reply-To: <131228124348.ZM27114@torch.brasslantern.com>
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
next prev parent reply other threads:[~2013-12-29 13:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-28 16:49 Chris Johnson
2013-12-28 20:43 ` Bart Schaefer
2013-12-29 13:35 ` Chris Johnson [this message]
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=20131229133554.GA46017@cs2666372x \
--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).