You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bash_preexec_hooks 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #! /usr/bin/env bash
  2. export __BASH_HOOKS_TIMEOUT__=60
  3. export __BASH_HOOKS_COMMAND__=
  4. export __BASH_HOOKS_TIME__=
  5. timestamp()
  6. {
  7. date "+%s"
  8. }
  9. preexec()
  10. {
  11. export __BASH_HOOKS_COMMAND__="$1"
  12. export __BASH_HOOKS_TIME__=$(timestamp)
  13. }
  14. precmd()
  15. {
  16. if [ -n "$__BASH_HOOKS_COMMAND__" ]
  17. then
  18. ts=$(timestamp)
  19. diff=$((ts - $__BASH_HOOKS_TIME__))
  20. __bash_hooks_command_finished "$__BASH_HOOKS_COMMAND__" $diff
  21. export __BASH_HOOKS_COMMAND__=
  22. export __BASH_HOOKS_TIME__=
  23. fi
  24. }
  25. displaytime()
  26. {
  27. local T=$1
  28. local D=$((T/60/60/24))
  29. local H=$((T/60/60%24))
  30. local M=$((T/60%60))
  31. local S=$((T%60))
  32. [[ $D > 0 ]] && printf '%d days ' $D
  33. [[ $H > 0 ]] && printf '%d hours ' $H
  34. [[ $M > 0 ]] && printf '%d minutes ' $M
  35. [[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and '
  36. printf '%d seconds\n' $S
  37. }
  38. __bash_hooks_command_finished()
  39. {
  40. if [ "$2" -ge "$__BASH_HOOKS_TIMEOUT__" ]
  41. then
  42. human=$(displaytime "$2")
  43. text="Command \"$1\" finished in $human ($2 seconds)"
  44. echo "$text"
  45. (nma $(hostname -f) "$1" "$text" >/dev/null 2>/dev/null &)
  46. fi
  47. }