mailrun 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env bash
  2. #
  3. # Program is a rip off of pbrisbin's mailrun
  4. # <https://github.com/pbrisbin/mutt-config/blob/master/bin/mailrun>
  5. #
  6. # This version, however, runs a number of short offlineimap runs of only the inbox
  7. # between executing full runs.
  8. QUICKRUN=1
  9. OPTIONS='-o'
  10. LOGFILE=''
  11. SHORTRUNS=3
  12. usage() {
  13. printf '%s\n' "$(basename $0) [go|log|next|status|help]"
  14. }
  15. # offlineimap has some issue joining threads and will occasionally hang
  16. # indefinitely. This method checks every 10 seconds if the process
  17. # identified as $1 is still running. After a number of checks
  18. # (~5 minutes for quick, ~15 minutes for normal), it kills it. Return
  19. # non-zero to indicate something was killed.
  20. monitor() {
  21. local pid=$1 i=0
  22. local time=30
  23. if (( QUICKRUN == 0 )); then
  24. time=90
  25. fi
  26. while ps $pid &>/dev/null; do
  27. if (( i++ > time )); then
  28. echo "Max checks reached. Sending SIGKILL to ${pid}..." >&2
  29. echo "[MAILRUN] Sending SIGKILL to ${pid}..." >> "$LOGFILE"
  30. kill -9 $pid; return 1
  31. fi
  32. sleep 10
  33. done
  34. return 0
  35. }
  36. check_runtype() {
  37. local count
  38. mkdir -p /tmp/mailrun
  39. LOGFILE="/tmp/mailrun/${RANDOM}"
  40. count=$(find /tmp/mailrun -type f | wc -l)
  41. if (( count < SHORTRUNS )); then
  42. OPTIONS="-q $OPTIONS -f 'INBOX'"
  43. else
  44. QUICKRUN=0
  45. rm -rf /tmp/mailrun
  46. fi
  47. mkdir -p /tmp/mailrun
  48. touch "$LOGFILE"
  49. }
  50. log() {
  51. latest="$(ls -rt /tmp/mailrun | tail -n 1)"
  52. tail -f "/tmp/mailrun/${latest}"
  53. }
  54. next_full() {
  55. local count=$(find /tmp/mailrun -type f | wc -l)
  56. local to_full=$(( SHORTRUNS - count ))
  57. printf 'Next full sync: %s runs\n' "$to_full"
  58. }
  59. status() {
  60. printf '== Next Run ==\n'
  61. next_full
  62. printf '== Timer ==\n'
  63. systemctl status --user offlineimap.timer
  64. printf '== Service ==\n'
  65. systemctl status --user offlineimap
  66. }
  67. parse_args() {
  68. while [ -n "$1" ]; do
  69. case "$1" in
  70. 'go')
  71. printf 'Doing mail run...\n'
  72. ;; # noop
  73. 'log')
  74. log
  75. exit 0
  76. ;;
  77. 'next')
  78. next_full
  79. exit 0
  80. ;;
  81. 'help')
  82. usage
  83. exit 0
  84. ;;
  85. 'status')
  86. status
  87. exit 0
  88. ;;
  89. *)
  90. usage
  91. exit 1
  92. ;;
  93. esac
  94. shift
  95. done
  96. }
  97. main() {
  98. parse_args "$@"
  99. read -r pid < ~/.offlineimap/pid
  100. if ps $pid &>/dev/null; then
  101. echo "Process $pid already running. Exiting..." >&2
  102. exit 1
  103. fi
  104. check_runtype
  105. printf '[MAILRUN] Starting\n' > "$LOGFILE"
  106. if command -v imapfilter > /dev/null 2>&1; then
  107. imapfilter &>> "$LOGFILE" & monitor $!
  108. fi
  109. printf 'offlineimap %s\n' "$OPTIONS" >> "$LOGFILE"
  110. offlineimap $OPTIONS &>> "$LOGFILE" & monitor $!
  111. }
  112. main "$@"