AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 726621
Accepted
SingularisArt
SingularisArt
Asked: 2022-11-29 10:49:28 +0800 CST2022-11-29 10:49:28 +0800 CST 2022-11-29 10:49:28 +0800 CST

无法让 systemd 运行脚本

  • 772

这是我的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那里得到了这段代码。

systemd
  • 1 1 个回答
  • 38 Views

1 个回答

  • Voted
  1. Best Answer
    SingularisArt
    2022-11-30T20:00:53+08:002022-11-30T20:00:53+08:00

    好吧。我已经找到了解决方案。这是新修改的脚本:

    #!/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.target
    
    • 0

相关问题

  • journalctl 中的区分级别

  • 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _

  • systemd:如何在服务启动时运行脚本,而不编辑服务定义

  • 使用 systemd 看门狗支持重新启动应用程序

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve