#!/bin/sh set -eu reap_proc() { local host="${1}" local user="${2}" shift 2 local pid= local t=0 { read -r pid <~/".cache/offlineimap/Account-${host}-${user}.pid"; } \ 2>/dev/null || return 0 while ps -p ${pid} -o cmd | grep -Fq offlineimap; do case ${t} in 0 | 10) kill ${pid} 1>/dev/null 2>&1 || return 0;; 20) return 1;; # Give up after 20 seconds. esac sleep 1 t=$((${t} + 1)) done return 0 } ping_srvr() { local host="${1}" local user="${2}" shift 2 # ping -c 1 -W 10 "${host}" || return 1 ssh -o ConnectTimeout=10 "${user}@${host}" exit 0 || return 1 return 0 } sync() { local host="${1}" local user="${2}" shift 2 reap_proc "${host}" "${user}" || return 1 ping_srvr "${host}" "${user}" || return 1 offlineimap -a "${host}-${user}" \ 1>>~/".cache/offlineimap/Account-${host}-${user}.log" 2>&1 & printf '%d\n' ${!} >~/".cache/offlineimap/Account-${host}-${user}.pid" } #sync 'pehjota.net' 'pj' || : sync 'libiquity.com' 'patrick.mcdermott' || :