]> git.rmz.io Git - dotfiles.git/blobdiff - bin/offlineimap.cron
offlineimap: cleanup rc + switch to cron
[dotfiles.git] / bin / offlineimap.cron
diff --git a/bin/offlineimap.cron b/bin/offlineimap.cron
new file mode 100755 (executable)
index 0000000..3c26e5f
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/bash
+# http://pbrisbin.com/posts/mutt_gmail_offlineimap/
+
+monitor() {
+    local pid=$1 i=0
+
+    while ps $pid &>/dev/null; do
+        if (( i++ > 5)); then
+            echo "Max checks reached. Sending SIGKILL to ${pid}..." >&2
+            kill -9 $pid; return 1
+        fi
+
+        sleep 10
+    done
+
+    return 0
+}
+
+read -r pid < ~/.offlineimap/pid
+
+if ps $pid &>/dev/null; then
+    echo "Process $pid already running. Exiting..." >&2
+    exit 1
+fi
+
+offlineimap -o & monitor $!
+