]> git.donarmstrong.com Git - bin.git/blob - getmail
handle missing pid and sleep longer before killing
[bin.git] / getmail
1 #!/bin/sh
2
3 # Configure the two things below for your MUA and the host being run.
4 IMAPHOST=linnode.donarmstrong.com
5 MUA=mutt
6
7 #set -x
8
9 # Don't check mail if ~/.nocheck exists and we're not running
10 # interactively.
11 if [ -z "$CRON" ] && [ -e ~/.nocheck ]; then
12     exit 0;
13 fi;
14
15 if [ -z "$HOSTNAME" ]; then
16     HOSTNAME="$(hostname)"
17 fi;
18
19 # if we're not running under cron or there is a mailserver running
20 if [ -z "$CRON" ] || kill -0 `pgrep -U $(id -u) -x $MUA` 2>/dev/null; then
21     # and we can ping the imap host
22     if ping -q -c 3 $IMAPHOST >/dev/null 2>&1; then
23         # source the ssh info if it exists
24         if [ -e ~/.ssh/"ssh_agent_info_$HOSTNAME" ]; then
25             . ~/.ssh/"ssh_agent_info_$HOSTNAME" > /dev/null;
26         fi;
27         PID="$(pgrep -U $(id -u) offlineimap)";
28         if [ -n "$PID" ]; then
29             # if offlineimap has been running for more than 20 minutes
30             if [ $(( $(date +'%s') - $(stat -c '%Y' /proc/$PID/cmdline) )) -gt 1200 ]; then
31                 kill "$PID" >/dev/null 2>&1 || true;
32                 sleep 5
33                 kill -0 "$PID" >/dev/null 2>&1 && kill -9 "$PID" >/dev/null 2>&1;
34                 PID="$(pgrep -U $(id -u) offlineimap)";
35             fi;
36         fi;
37         if [ -z "$PID" ]; then
38             if nice -n 19 offlineimap -o -u Noninteractive.Basic 2>&1 |grep validity| \
39                 perl -ne 'my ($account,$list) = /UID validity problem for folder ([\w-]+)\.([\w-]+)/;
40                       next unless defined $account and length $account;
41                       qx(/home/don/bin/deletemailbox -a $account $list > /dev/null);'; then
42             # Clean the lists up too
43                 for account in bugs lists; do
44                     find /home/don/Mail/$account -maxdepth 1 -mindepth 1 -type d \
45                         -exec sh -c 'test $(find "{}" -type f |wc -l) -eq 0' ';' \
46                         -printf '%f\000'|xargs -0 -rn 1 ~/bin/deletemailbox -a $account >/dev/null 2>&1;
47                     find /home/don/Mail/$account -maxdepth 2 -mindepth 2 -iname 'new' \
48                         -type d -ctime +60 -printf '%h\n'|xargs -rn 1 basename|xargs \
49                         -rn 1 /home/don/bin/deletemailbox -a $account >/dev/null 2>&1;
50                 done;
51             fi;
52         fi;
53     fi;
54 fi;