Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

bash_preexec_hooks 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. send_notif=1
  43. while read exp
  44. do
  45. if [ $(echo "${1}" | grep -c "${exp}") -ge 1 ]
  46. then
  47. send_notif=0
  48. fi
  49. done < ~/.nma_exclude
  50. human=$(displaytime "$2")
  51. text="Command \"$1\" finished in $human ($2 seconds)"
  52. if [ ${send_notif} -eq 1 ]
  53. then
  54. text="${text} (notified)"
  55. fi
  56. echo "$text"
  57. if [ ${send_notif} -eq 1 ]
  58. then
  59. (nma $(hostname -f) "$1" "$text" >/dev/null 2>/dev/null &)
  60. fi
  61. fi
  62. }