有没有办法让 zsh 在 REPORTTIME 之后运行命令?

jck*_*jck 7 zsh

如果一个进程花费的时间大于 REPORTTIME,zsh 会在它完成时打印时间。除此之外,有没有办法让它运行自定义命令?(我想使用通知发送让我知道进程已完成执行)

hdg*_*ood 5

我只是有同样的想法。做了一个快速的谷歌并找到了这个;我稍微调整了一下。把它塞进你的 zshrc:

if [[ -x `which notify-send` ]]; then
    notify-preexec-hook() {
        zsh_notifier_cmd="$1"
        zsh_notifier_time="`date +%s`"
    }

    notify-precmd-hook() {
        local time_taken

        if [[ "${zsh_notifier_cmd}" != "" ]]; then
            time_taken=$(( `date +%s` - ${zsh_notifier_time} ))
            if (( $time_taken > $REPORTTIME )); then
                notify-send "task finished" \
                    "'$zsh_notifier_cmd' exited after $time_taken seconds"
            fi
        fi
        zsh_notifier_cmd=
    }
fi

[[ -z $preexec_functions ]] && preexec_functions=()
preexec_functions=($preexec_functions notify-preexec-hook)

[[ -z $precmd_functions ]] && precmd_functions=()
precmd_functions=($precmd_functions notify-precmd-hook)
Run Code Online (Sandbox Code Playgroud)

我比较满意!:)