OfflineIMAP with systemd
Posted on

OfflineIMAP is a mail synchronization utility that allows you to download mail over IMAP and save it locally as a Maildir. It’s useful if you want to read your mail with a Mail User Agent (MUA) such as Mutt. Setting up OfflineIMAP has been explained nicely elsewhere:

This is just a quick post that explains how OfflineIMAP can be integrated into a user session controlled by systemd.

0.1 Periodic synchronization with systemd

I’ve encountered problems trying to run OfflineIMAP continuously as a daemon. It would often freeze and not synchronize mail for hours. The most common solution to this is to start OfflineIMAP periodically with cron and only let it synchronize once on each run.

This is what I had been doing for over a year, until I’ve recently switched to the following systemd-based solution: I run systemd with the --user switch inside my user session, allowing me to configure services (like OfflineIMAP) very conveniently. Creating the following unit file in ~/.config/systemd/user allows you to start a single synchronization.

# offlineimap.service

[Unit]
Description=OfflineIMAP Quicksync
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/offlineimap -o -q -u quiet
TimeoutStartSec=1min30s

Additionally, a second .timer unit is needed to run the service every 30 seconds:

# offlineimap.timer

[Unit]
Description=OfflineIMAP Quicksync timer

[Timer]
OnCalendar=*:*:0/30
Unit=offlineimap.service

[Install]
WantedBy=default.target

You can now enable mail synchronization by executing

systemctl --user enable offlineimap.timer

See if OfflineIMAP is working correctly by following the journal:

journalctl -f