是否有与 zsh 的 precmd 等效的命令,用于在命令完成后执行某个功能?
我正在寻找一种方法,当任何进程耗时超过 60 秒时,我都可以向自己发送通知。如果命令耗时超过 REPORTTIME 秒数中设置的值,Zsh 可以很好地打印每个命令所用资源的摘要。据我所知,唯一可用的选项是以 TIMEFMT 格式打印。
(更具体地说,我希望向自己发送一条通知https://pushover.net/如果在分离的 tmux 会话中运行的任何进程需要超过 60 秒才能完成。)
答案1
答案2
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 )) &&
print -r -- "'$stats[3,-1]' took $time seconds"
# Replace the `print` statement above with your notification call.
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