7

If a process takes greater than REPORTTIME, zsh prints the time when it finishes. Is there a way to make it run a custom command in addition to this?(I would like to use notify send to let me know the process finished executing)

1 Answer 1

5

I just had the same thought. Did a quick google and found this; I've adapted it a bit. Shove this into your 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)

I'm rather happy with it! :)

3
  • Welcome to Super User! Generally we like answers on the site to be able to stand on their own - Links are great, but if that link ever breaks the answer should have enough information to still be helpful. Please consider editing your answer to include more detail. See the FAQ for more info.
    – slm
    Apr 5, 2013 at 19:56
  • @slm -- that makes a lot of sense. edited
    – hdgarrood
    Apr 5, 2013 at 20:09
  • 1
    FYI it's simpler to add a function hook via add-zsh-hook as described here: zsh.sourceforge.net/Doc/Release/…
    – wting
    Apr 8, 2017 at 6:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .