Complete.Org: Mailing Lists: Archives: offlineimap: November 2007:
Re: OfflineIMAP and launchd
Home

Re: OfflineIMAP and launchd

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: offlineimap@xxxxxxxxxxxx
Subject: Re: OfflineIMAP and launchd
From: Raffi Khatchadourian <khatchad@xxxxxxxxxxxxxxxxxx>
Date: Thu, 22 Nov 2007 11:27:46 -0500

Well, as the story goes, I used to run OfflineIMAP on Macbook running
Mac OS X 10.4 (Tiger) via a cron job with the following command:

offlineimap.py -q -u Noninteractive.Quiet

*without* the 'autorefresh' option enabled in my .offlineimaprc file.
After I upgraded from 10.4 to 10.5 (Leopard) I was quite surprised to
see that my entire crontab had disappeared! After some research, I
discovered that cron had be deprecated in OS X in favor of launchd
(http://en.wikipedia.org/wiki/Launchd). Its still possible to use cron
anyway, however, launchd has quite a few features that I thought may be
desirable for notebook users, specifically I was hoping that it would
deal with the computer going to sleep on a frequent basis and be able to
recover the offlineimap process accordingly better than cron had. As
such, I gave it a shot. 

The first thing I did was download a GUI program
that makes working with launchd very easy called Lingon
(http://lingon.sourceforge.net). I created a "user agent" called
com.khatchadourian.raffi.offlineimap using Lingon that essentially
created the file
~/Library/LaunchAgents/com.khatchadourian.raffi.OfflineIMAP.plist with
the following contents (I added the debug value so that I can see any
errors on the system log using Console):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd";>
<plist version="1.0">
<dict>
        <key>Debug</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
        <key>Label</key>
        <string>com.khatchadourian.raffi.offlineimap</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Users/raffi/local/offlineimap/offlineimap.py</string>
                <string>-q</string>
                <string>-u</string>
                <string>Noninteractive.Quiet</string>
        </array>
        <key>RunAtLoad</key>
        <false/>
        <key>StartInterval</key>
        <integer>120</integer>
</dict>
</plist>

Everything above is pretty self-explanatory. Basically, the agent runs
when I log in and thereafter every 2 minutes.  After a log in/log out,
everything seemed to work fine except that the offlineimap process would
hang when the computer would awake from sleep.  Therefore, I would have
to manually kill the process. After some further research, I found a
nifty daemon for OS X that executes scripts every time the computer
either wakes up or falls asleep called sleepwatcher
(http://www.bernhard-baehr.de). I then made the following bash script
and named it ~/.wakeup

#!/bin/bash
/bin/launchctl stop com.khatchadourian.raffi.offlineimap
/bin/rm -f /Users/raffi/.offlineimap/lock /Users/raffi/.offlineimap/pid
/bin/launchctl start com.khatchadourian.raffi.offlineimap
ps -ef | grep "offlineimap" | grep -v grep | awk '{ print $2 }' | xargs kill 
-HUP

I'm not sure if every line is essential for correct functionality, but
as is it works. Line 2 calls the launchd controller utility (launchctl)
to, upon wake up, stop my offlineimap job. Next on line 3, a couple of
files are removed from the offlineimap meta directory so that
offlineimap, when restarted, doesn't complain about a previous instance
being executed with the same meta data. Next on line 4 I restart the
job. Finally, just in case that didn't work, on line 5 I manually kill
the offlineimap process. Again, this is not an exact science and I'm
sure this script can be improved. The result, however, is the following:

1) I don't need to run offlineimap in a separate terminal window, it
    runs in the background chronologically.
2) Unlike cron, any errors are not emailed to me (which of course can be
    good or bad), instead, they are in the system logs (like all other
    errors).
3) I never have to manually kill the offlineimap process every time I
    lift the lid of my computer. I wait a few seconds and wala my email
    is in my Inbox.

That about sums it up. Let me know if you have any questions!

On Mon 19.Nov'07 at 10:13:43 -0800, David L. Emerson wrote:
>I don't need such a thing myself, but it would be very nice to have
>such a solution in the archives. Someday in the future, someone will
>need it ;)
>
>On Monday 19 November 2007 8:32 am, Raffi Khatchadourian wrote:
>> Don't mean to reply to my own email, but I have found a solution to
>> this problem if anyone is interested, let me know!
>> 
>> Raffi
>> 
>> On Thu  8.Nov'07 at  9:00:46 -0500, Raffi Khatchadourian wrote:
>> >Has anyone had any success running OfflineIMAP from launchd (now
>> >that crontab is deprecated on OS X)? I somehow can't get the logging
>> >to work and often when I put my notebook computer to sleep, when the
>> >computer awakes offlineimap is not functioning via the launchd job
>> >(the pid and lock files need to be removed from the ~/.offlineimap
>> >directory). 



[Prev in Thread] Current Thread [Next in Thread]