#! /usr/bin/env bash export __BASH_HOOKS_TIMEOUT__=60 export __BASH_HOOKS_COMMAND__= export __BASH_HOOKS_TIME__= timestamp() { date "+%s" } preexec() { export __BASH_HOOKS_COMMAND__="$1" export __BASH_HOOKS_TIME__=$(timestamp) } precmd() { if [ -n "$__BASH_HOOKS_COMMAND__" ] then ts=$(timestamp) diff=$(($ts - $__BASH_HOOKS_TIME__)) __bash_hooks_command_finished "$__BASH_HOOKS_COMMAND__" $diff export __BASH_HOOKS_COMMAND__= export __BASH_HOOKS_TIME__= fi } displaytime() { local T=$1 local D=$((T/60/60/24)) local H=$((T/60/60%24)) local M=$((T/60%60)) local S=$((T%60)) [[ $D > 0 ]] && printf '%d days ' $D [[ $H > 0 ]] && printf '%d hours ' $H [[ $M > 0 ]] && printf '%d minutes ' $M [[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' printf '%d seconds\n' $S } __bash_hooks_command_finished() { if [ "$2" -ge "$__BASH_HOOKS_TIMEOUT__" ] then send_notif=1 while read exp do if [ $(echo "${1}" | grep -c "${exp}") -ge 1 ] then send_notif=0 fi done < ~/.nma_exclude human=$(displaytime "$2") text="Command \"$1\" finished in $human ($2 seconds)" if [ ${send_notif} -eq 1 ] then text="${text} (notified)" fi echo "$text" if [ ${send_notif} -eq 1 ] then (nma $(hostname -f) "$1" "$text" >/dev/null 2>/dev/null &) fi fi }