Using launchctl on Mac OSX 10.10 to run a script periodically

Lately I’ve been playing around with mutt, a “small but powerful text-based email client for Unix operating systems” and offlineimap, a software that downloads your mailbox as a local Maildir.

After I finally got everything set up (easier said than done), I needed a way to make offlineimap synchronize my mailbox periodically. Apple recommends using launchctl instead of crontab. The following example shows how to run offlineimap in quiet mode every two minutes:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

This file is basically an XML file, but the extension you must use is .plist. You would typically call it something like com.yourname.scriptname.plist. Place it in the folder ~/Library/LaunchAgents/, and make sure you replace yourname and scriptname with something relevant.

When you’ve saved the file to the specified location, type the following to load it into launchctl:

launchctl load ~/Library/LaunchAgents/com.yourname.scriptname.plist

If you ever need to remove it from launchctl, you can type the following:

launchctl remove ~/Library/LaunchAgents/com.yourname.scriptname.plist

Receive a weekly update in your inbox

I'd very much appreciate it if you'd be interested in getting an email delivered directly to your inbox every Sunday morning at 9AM with a summary of my tech related blog posts from the last week. I will not in any way use your email address for anything else. That's a promise.

Leave a Comment