Skip to content

Instantly share code, notes, and snippets.

@novalore
Last active August 29, 2015 13:56
Show Gist options
  • Save novalore/9112686 to your computer and use it in GitHub Desktop.
Save novalore/9112686 to your computer and use it in GitHub Desktop.
Mailsync.sh: a simple wrapper for offlineimap and notmuch
#!/bin/bash
#**************************************************
#*This is free software; you can redistribute it *
#*and/or modify it under the terms of the GNU *
#*General Public License as published by the Free *
#*Software Foundation; either version 3 of the *
#*License, or (at your option) any later version. *
#**************************************************
# Author: Lorenzo Novaro (novalore@19.coop)
#Set the wallet in use:
wallet=kdewallet
#Time format is a matter of taste
time=$(date +"%x %X")
#Set logfile
logfile=~/.mailsync/mailsync.log
#Let's start...
#Use flock to check that mailsync is not already running
#if it's running, wait for lock on /var/lock/.mailsync.lock (fd 200) for 9 seconds before dying
(
flock -x -w 9 200
if [ "$?" != "0" ]; then
echo "$time - Doing nothing: script already running" >> $logfile; exit 1;
else
if [ `/usr/bin/qdbus org.kde.kwalletd /modules/kwalletd org.kde.KWallet.isOpen $wallet` = true ]; then
/usr/bin/offlineimap -u quiet && /usr/bin/notmuch new;
else
echo "$time - Doing nothing: kwallet not open..." >> $logfile; exit 1;
fi
fi
) 200>/var/lock/.mailsync.lock
exit 0;
@novalore
Copy link
Author

If you want to use cron to run this script, make sure that cron is able to communicate with dbus.

Example of working crontab line (execute every 5 mins):

    */5 *   *   *   *   export DISPLAY=:0 && /home/user/mailsync.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment