如果终端命令花费的时间超过 x 秒,如何发送通知?

how to send a notification if a terminal command takes more than x seconds?

我正在寻找一种方法来向自己发送任何超过 60 秒的进程的通知。

要发送通知,我可以使用类似

的东西
notify-send -t 1 "hey command finished"

有什么方法可以将 config 文件保存在某处或在我的 zsh 中自动执行此行为?

类似于此unanswered question from a different website

添加到您的 .zshrc 文件:

notify() {
  emulate -L zsh  # Reset shell options inside this function.

  # Fetch the last command with elapsed time from history:
  local -a stats=( "${=$(fc -Dl -1)}" )
  # = splits the string into an array of words.
  # The elapsed time is the second word in the array.

  # Convert the elapsed minutes (and potentially hours) to seconds:
  local -a time=( "${(s.:.)stats[2]}" )
  local -i seconds=0 mult=1
  while (( $#time[@] )); do
    (( seconds += mult * time[-1] ))
    (( mult *= 60 ))
    shift -p time
  done

  (( seconds >= 60 )) && 
      notify-send -t 1 \
          "hey command '$stats[3,-1]' finished in $seconds seconds"

  return 0  # Always return 'true' to avoid any hiccups.
}

# Call the function above before each prompt:
autoload -Uz add-zsh-hook
add-zsh-hook precmd notify