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
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! :)
-
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.– slmApr 5, 2013 at 19:56
-
-
1FYI it's simpler to add a function hook via
add-zsh-hook
as described here: zsh.sourceforge.net/Doc/Release/…– wtingApr 8, 2017 at 6:05