-
Notifications
You must be signed in to change notification settings - Fork 115
/
mbsyncd
executable file
·82 lines (63 loc) · 2.37 KB
/
mbsyncd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# mbsync seems to work well but, weirdly, doesn't have a daemon mode
# (surely 95% of users will want to run it that way?)
# Here's a wrapper for it that forks into background
# and runs forever, printing the time each time.
# XXX WISHLIST: catch a signal that starts a refresh.
# Actually, might this happen automatically
# because it would wake up from sleep?
from __future__ import print_function
import time
import subprocess
import os, sys
# Change this to whereever you want the log:
logfile = os.path.expanduser("~/.mutt/mbsync.log")
def Usage():
print("Usage: %s [seconds]" % os.path.basename(sys.argv[0]))
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) > 2:
Usage()
if len(sys.argv) == 2:
try:
secs = int(sys.argv[1])
except:
Usage()
else:
secs = 120
# Is an mbsync or mbsyncd process already running?
# Running two at once would be bad.
PROCDIR = '/proc'
for proc in os.listdir(PROCDIR):
if not proc[0].isdigit():
continue
# Is it the current process? Obviously that doesn't count.
if int(proc) == os.getpid():
continue
# Race condition: processes can come and go, so we may not be
# able to open something just because it was there when we
# did the listdir.
try:
with open(os.path.join(PROCDIR, proc, 'cmdline')) as procfp:
procline = procfp.readline()
if "mbsync" in procline:
print("There's already an mbsync running, pid %s: %s"
% (proc, ' '.join(procline.split('\0'))))
sys.exit(1)
except Exception as e:
print("Exception", e)
pass
# Fork and run in the background.
rc = os.fork()
if rc:
sys.exit(0)
logfp = open(logfile, 'w')
print("mbsync daemon, time %d, ouput to %s" % (secs, logfile))
while True:
subprocess.call(["date"], stdout=logfp, stderr=logfp)
# mbsync suppresses its summary when stdout isn't a tty,
# and there's no way to request a summary.
# So nothing will go to the log; but if output isn't redirected,
# it will print its summary to the terminal every time.
subprocess.call(["mbsync", "-a"], stdout=logfp, stderr=logfp)
time.sleep(secs)