无法让 systemd 运行脚本

无法让 systemd 运行脚本

这是我的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!" "

答案1

好吧。我已经想出了解决办法。这是新修改的脚本:

#!/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" "

相关内容