Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Is there a way to put a timeout on a function?
- X-seq: zsh-workers 47361
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Rudi C <rudiwillalwaysloveyou@xxxxxxxxx>
- Subject: Re: Is there a way to put a timeout on a function?
- Date: Fri, 4 Sep 2020 14:14:50 -0700
- Archived-at: <https://zsh.org/workers/47361>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-09/CAH%2Bw%3D7Z_qtMaz4C7BR%2B0SQL1k2Dk8Ej5DRYhez5LtQLbdh1Prw%40mail.gmail.com>
- Authentication-results: zsh.org; iprev=pass (mail-ot1-f52.google.com) smtp.remote-ip=209.85.210.52; dkim=pass header.d=brasslantern-com.20150623.gappssmtp.com header.s=20150623 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none
- Cc: "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=3svnPav8mpokp3XFzXDnglJM7hfMClnb2O+lkBctC2s=; b=0b0Lv/vaxDCQIvS6q4PuAFpQwG6sXF1CCQIQb/ZgKYeT/MnryAoHFdRupMfslZdpSm +cCitKYK5AaxM98FLye+EeiOeHOMRgKxK0OYzS1er92hZbyQSjcKsCL8Cyw5A9gh+KC2 ZWW2Jv9XmqXnzZ4CvSR4QUiNURTCJ4YG4JdIMBCf+76QjLSS5flvKCxRKtFz0nsFRIQr 7tEjMWfQhnqNaXEG/xubz+L1J1AdF7cEEMyVwFUdpXoCr9oliuAFL/oWNUYNNpnkEaq9 Tdr0+ogEMH+r+IkBKCbk9seM3SvsYKIi0EwS6GwitmJuzzEIR+O3WjEtDcZrm1LVGmgW xQow==
- In-reply-to: <CAE9z9A21U0Pye29dLAbeyYGW-V1pdHBr6vA+LBTho8wvU9+Jqw@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-workers.zsh.org>
- List-owner: <mailto:zsh-workers-request@zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
- References: <CAE9z9A21U0Pye29dLAbeyYGW-V1pdHBr6vA+LBTho8wvU9+Jqw@mail.gmail.com>
- Sender: zsh-workers-request@xxxxxxx
On Mon, Aug 31, 2020 at 8:00 AM Rudi C <rudiwillalwaysloveyou@xxxxxxxxx> wrote:
>
> I need to limit the execution time of arbitrary zsh functions. Is there a way to achieve this? I'm ok with just adding a time limit to all the external commands, but I do not know how to do that. (I don't want to manually prefix all my commands with the GNU timeout command.)
Here's a basic outline; you'll have to fiddle with it to get the
details as you want:
# This is the default, but for clarity
setopt DEBUG_BEFORE_CMD
TRAPDEBUG() {
# Prevent the command from running twice
setopt localoptions ERR_EXIT
# Set up the timeout
sleep $DESIRED_TIMEOUT &
timer=$!
# Background so parent can wait for timer
(){ eval "${ZSH_DEBUG_CMD}"; kill $timer } &
victim=$!
wait $timer && kill $victim
}
Note that this prevents anything from changing the state of the
current shell, so be careful experimenting with it. One of the things
you probably need to "fiddle with" is to have this function return
BEFORE it sets ERR_EXIT for any command that SHOULD run in the current
shell. Another is that this buries the exit status of the command, so
you need a way to capture it.
> On a sidenote, I would also like to make the shell stop forcefully (like when a glob fails) if it doesn't find an external command. I thought maybe these two problems might have similar solutions. Is prexec viable for them? Does preexec even work for noninteractive scripts?
Preexec and precmd are strictly for interactive shells.
Another potentially dangerous solution:
command_not_found_handler() {
builtin print "command not found: $*"
builtin kill -USR1 $$
}
TRAPUSR1() { exit 1 }
Aside to -workers: I've had this go into an infinite recursion if the
TRAPUSR1 definition precedes the command_not_found_handler definition
and/or the "builtin" prefix is not used. This seems odd, but is not
reliably reproducible.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author