Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #!/usr/bin/env zsh
  2.  
  3. # This script prints a bell character when a command finishes
  4. # if it has been running for longer than $zbell_duration seconds.
  5. # If there are programs that you know run long that you don't
  6. # want to bell after, then add them to $zbell_ignore.
  7. #
  8. # This script uses only zsh builtins so its fast, there's no needless
  9. # forking, and its only dependency is zsh and its standard modules
  10. #
  11. # Written by Jean-Philippe Ouellet <jpo@vt.edu>
  12. # Made available under the ISC license.
  13.  
  14. # only do this if we're in an interactive shell
  15. [[ -o interactive ]] || return
  16.  
  17. # get $EPOCHSECONDS. builtins are faster than date(1)
  18. zmodload zsh/datetime || return
  19.  
  20. # make sure we can register hooks
  21. autoload -Uz add-zsh-hook || return
  22.  
  23. # initialize zbell_duration if not set
  24. (( ${+zbell_duration} )) || zbell_duration=60
  25. (( ${+zbell_duration_email} )) || zbell_duration_email=180
  26.  
  27. # initialize zbell_ignore if not set
  28. (( ${+zbell_ignore} )) || zbell_ignore=($EDITOR $PAGER ls watch htop top ssh iotop dstat vmstat nano emacs vi bwm-ng less more fdisk audacious play aplay sqlite3 wine mtr ping traceroute vlc mplayer smplayer tail tmux screen man sawfish-config powertop g)
  29.  
  30. # initialize it because otherwise we compare a date and an empty string
  31. # the first time we see the prompt. it's fine to have lastcmd empty on the
  32. # initial run because it evaluates to an empty string, and splitting an
  33. # empty string just results in an empty array.
  34. zbell_timestamp=$EPOCHSECONDS
  35.  
  36. # right before we begin to execute something, store the time it started at
  37. zbell_begin() {
  38. zbell_timestamp=$EPOCHSECONDS
  39. zbell_lastcmd=$1
  40. }
  41.  
  42. zbell_noise() {
  43. play -q /home/wonko/Seafile/Phone/media/audio/notifications/Bazinga.mp3 &|
  44. }
  45.  
  46. zbell_email() {
  47. curl --ssl-reqd \
  48. --url "smtps://mail.patshead.com:465" \
  49. --mail-from "zbell@patshead.com" \
  50. --mail-rcpt "zbell@patshead.com" \
  51. --user 'zbell@patshead.com:password' \
  52. --insecure --upload-file - &> /dev/null <<EOF &|
  53. From: "ZSH Notification" <zbell@patshead.com>
  54. To: "Pat Regan" <thehead@patshead.com>
  55. Subject: $HOST - $zbell_lastcmd
  56.  
  57. Completed with exit status $zbell_exit_status
  58.  
  59.  
  60. Love,
  61.  
  62. Zbell
  63.  
  64. EOF
  65. }
  66.  
  67. # when it finishes, if it's been running longer than $zbell_duration,
  68. # and we dont have an ignored command in the line, then print a bell.
  69. zbell_end() {
  70. zbell_exit_status=$?
  71. ran_long=$(( $EPOCHSECONDS - $zbell_timestamp >= $zbell_duration ))
  72.  
  73. local has_ignored_cmd=0
  74. local zbell_lastcmd_tmp
  75. zbell_lastcmd_tmp="$zbell_lastcmd"
  76. regexp-replace zbell_lastcmd_tmp '^sudo ' ''
  77.  
  78. if [[ $zbell_last_timestamp == $zbell_timestamp ]]; then
  79. return
  80. fi
  81.  
  82. if [[ $zbell_lastcmd_tmp == "" ]]; then
  83. return;
  84. fi
  85.  
  86. zbell_last_timestamp=$zbell_timestamp
  87.  
  88. for cmd in ${(s:;:)zbell_lastcmd_tmp//|/;}; do
  89. words=(${(z)cmd})
  90. util=${words[1]}
  91. if (( ${zbell_ignore[(i)$util]} <= ${#zbell_ignore} )); then
  92. has_ignored_cmd=1
  93. break
  94. fi
  95. done
  96.  
  97. if (( ! $has_ignored_cmd )) && (( ran_long )); then
  98. local zbell_cmd_duration
  99. zbell_cmd_duration=$(( $EPOCHSECONDS - $zbell_timestamp ))
  100. if [[ $zbell_cmd_duration -gt $zbell_duration_email ]]; then
  101. zbell_email
  102. else
  103. zbell_noise
  104. fi
  105. notify-send "Job completed on $HOST:" "$zbell_lastcmd"
  106. print -n "\a"
  107. fi
  108. }
  109.  
  110. # register the functions as hooks
  111. add-zsh-hook preexec zbell_begin
  112. add-zsh-hook precmd zbell_end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement