下面是位于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.我是不是遗漏了什么?我从卢克·史密斯得到了这段代码。
发布于 2022-11-30 04:00:53
好的。我已经想出了解决办法。下面是新修改的脚本:
#!/bin/sh
num=$(ps aux | pgrep "mbsync" -c)
if [ "$num" -gt 0 ]; then
echo "mbsync is already running."; exit ;
fi
export HOME=/home/singularis
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export GNUPGHOME="$XDG_DATA_HOME/gnupg"
export PASSWORD_STORE_DIR="$XDG_CONFIG_HOME/password-store"
export MBSYNCRC="$XDG_CONFIG_HOME/mbsync/mbsyncrc"
export NOTMUCH_CONFIG="$XDG_CONFIG_HOME/notmuch/config"
export LASTRUN="$XDG_CONFIG_HOME/mailsynclastrun"
mbsync() {
command mbsync --config="$MBSYNCRC" "$@"
}
notmuch() {
command notmuch --config="$NOTMUCH_CONFIG" "$@"
}
notify() {
notify-send \
--icon="/usr/share/icons/oomox-xresources-reverse-flat/status/scalable/mail-unread-symbolic.svg" \
--app-name="neomutt" \
"$1" "$2"
}
new_notify() {
notify "New Mail" " $2 new mail(s) in \`$1\` account."
}
syncandnotify() {
acc=$(basename "$1")
opts="$2"
if [ "$opts" = "" ]; then
mbsync "$acc"
else
mbsync "$opts" "$acc"
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)
[ "$newcount" -gt 0 ] && new_notify "$acc" "$newcount"
}
if [ "$#" -gt "0" ]; then
for arg in "$@"; do
first_char="${arg%"${arg#?}"}"
[ "$first_char" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
done
accounts=$*
fi
if [ "$accounts" = "" ]; then
accounts=$(find "$HOME"/.local/share/mail -mindepth 1 -maxdepth 1 -type d \( ! -iname ".*" \))
fi
for account in $accounts; do
notify "Syncing $(basename "$account")"
syncandnotify "$account"
done
wait
notmuch new --quiet
touch "$LASTRUN"下面是我的两个mailsync.service和mailsync.timer文件:
[Unit]
Description=Mail sync
[Service]
ExecStart=/home/singularis/.local/bin/systemd/mailsync
Environment="HOME=/home/singularis"
RestartSec=900
[Install]
WantedBy=default.target[Unit]
Description=Mail sync
[Timer]
OnBootSec=15m
OnUnitActiveSec=15m
Persistent=true
[Install]
WantedBy=timers.targethttps://unix.stackexchange.com/questions/726621
复制相似问题