#!/bin/bash # http://pbrisbin.com/posts/mutt_gmail_offlineimap/ monitor() { local pid=$1 i=0 while ps $pid &>/dev/null; do if (( i++ > 10)); 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 $!