这是我的mailsync.service
和mailsync.timer
文件位于~/.config/systemd/user/
:
[Unit]
Description=Mail sync
[Service]
Type=oneshot
Nice=10
ExecStart=/home/singularis/.local/bin/systemd/mailsync
Environment="HOME=/home/singularis"
[Unit]
Description=Sync emails with mbsync on schedule
[Timer]
OnBootSec=2m
OnUnitActiveSec=2m
Persistent=true
[Install]
WantedBy=timers.target
内容如下/home/singularis/.local/bin/systemd/mailsync
:
#!/bin/sh
MBSYNCRC="$XDG_CONFIG_HOME/mbsync/mbsyncrc"
alias mbsync="mbsync -c $XDG_CONFIG_HOME/mbsync/mbsyncrc"
# Run only if not already running in other instance
pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
export GPG_TTY="$(tty)"
[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
[ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config"
lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/neomutt/.mailsynclastrun"
# Settings are different for MacOS (Darwin) systems.
case "$(uname)" in
Darwin)
notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
;;
*)
case "$(readlink -f /sbin/init)" in
*systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
esac
# remember if a display server is running since `ps` doesn't always contain a display
pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))"
displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
export DISPLAY=$x
notify-send --app-name="mutt-wizard" "New mail!" "? $2 new mail(s) in \`$1\` account."
done ;}
;;
esac
# Check account for new mail. Notify if there is new content.
syncandnotify() {
acc="$(echo "$account" | sed "s/.*\///")"
if [ "$1" = "pop" ]; then
# Handle POP
mpop "$acc"
else
# Handle IMAP
if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
fi
new=$(find\
"$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/ \
"$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
-type f -newer "$lastrun" 2> /dev/null)
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
[ -z "$MAILSYNC_MUTE" ] && case 1 in
$((newcount > 0)) ) notify "$acc" "$newcount" ;;
esac
}
# Sync accounts passed as argument or all.
if [ "$#" -gt "0" ]; then
for arg in "$@"; do
[ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
done
accounts=$*
fi
[ -z "$accounts" ] && accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC" 2>/dev/null)"
[ -z "$pop_accounts" ] && [ -x $MPOPRC ] && pop_accounts="$(awk '/^account/ {print $2}' "$MPOPRC" 2>/dev/null)"
# Parallelize multiple accounts
for account in $accounts; do
syncandnotify "imap" &
done
for account in $pop_accounts; do
syncandnotify "pop" &
done
wait
notmuch new --quiet
#Create a touch file that indicates the time of the last run of mailsync
touch "$lastrun"
这是输出journalctl --user -xeu mailsync.service
:
Nov 28 10:44:16 singularis systemd[871]: Failed to start Mail sync.
░░ Subject: A start job for unit UNIT has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit UNIT has finished with a failure.
░░
░░ The job identifier is 1336 and the job result is failed.
我错过了什么吗?我从Luke Smith那里得到了这段代码。
好吧。我已经找到了解决方案。这是新修改的脚本:
这是我的两个
mailsync.service
和mailsync.timer
文件: