OSX 和 launchctl,rsync/ssh 找不到密钥

OSX and launchctl, rsync/ssh can't find key

所以我一直在尝试为 rsync 编写一个 launchctl 守护进程,以便它每晚远程备份我的笔记本电脑。

launchctl守护进程运行良好,它使用root用户调用rsync,并指示rsync使用ssh,并从我的用户目录中获取密钥文件。这就是它变得有趣的地方。无论我做什么,ssh 都会抛出以下错误:rsync: Failed to exec ssh -i /Users/anthony/.ssh/id_rsa: No such file or directory (2)

钥匙确实在那儿。如果我从命令行单独调用 ssh,我可以从我的用户帐户和 root 帐户使用该密钥。我假设这与 launchctl 的范围或特权有关?下面是 launchctl 使用的 plist 文件。非常感谢您对此提供帮助。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>UserName</key>
    <string>root</string>
    <key>Label</key>
    <string>com.anthony.remoteBackup</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>23</integer>
            <key>Minute</key>
            <integer>00</integer>
        </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/rsync</string>
        <string>-azb</string>
        <string>--exclude=.*</string>
        <string>--exclude=.*/</string>
        <string>--backup-dir="/home/anthony/Documents/Old Remote Backup/macbook_air/"</string>
        <string>--suffix=.old</string>
        <string>--delete</string>
        <string>--delete-excluded</string>
        <string>-e "ssh -i /Users/anthony/.ssh/id_rsa"</string>
        <string>/Users/anthony/Documents</string>
        <string>anthony@remote_domain_name:"/home/anthony/Documents/Remote Backups/macbook_air/"</string>
    </array>
    <key>StandardOutPath</key>
    <string>/tmp/remote_backup_test</string>
    <key>StandardErrorPath</key>
    <string>/tmp/remote_backup_test</string>
</dict>
</plist>

这是 remote_backup_test

的标准误差
rsync: Failed to exec ssh -i /Users/anthony/.ssh/id_rsa: No such file or directory (2)
rsync error: error in IPC code (code 14) at /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-54.120.1/rsync/pipe.c(86) [sender=2.6.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-54.120.1/rsync/io.c(453) [sender=2.6.9]

我怀疑问题出在您传递参数的方式上。你在几个参数中的 double-quotes 是 shell 语法,但是 ProgramArguments 数组的元素不会被 shell 解析,所以它们不应该在那里.最重要的是,-e 之后的 space 旨在由 shell 解析为参数定界符,因此替换为:

        <string>-e "ssh -i /Users/anthony/.ssh/id_rsa"</string>

与:

        <string>-e</string>
        <string>ssh -i /Users/anthony/.ssh/id_rsa</string>

并从 --backup-dir= 和目标 (anthony@...) 参数中删除 double-quotes。