【问题标题】:how to send a notification if a terminal command takes more than x seconds?如果终端命令花费的时间超过 x 秒,如何发送通知?
【发布时间】:2021-08-09 22:46:55
【问题描述】:

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

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

notify-send -t 1 "hey command finished"

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

类似于这个unanswered question from a different website

【问题讨论】:

    标签: unix terminal zsh


    【解决方案1】:

    添加到您的.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
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2021-06-13
      • 2015-12-22
      • 2016-05-05
      • 2023-01-11
      • 2023-01-30
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多