LaunchAgent 脚本无法写入外部驱动器

LaunchAgent script can't write to external drive

macOS 卡特琳娜

我有一个 python 脚本,可以将文件写入外部驱动器。如果我手动 运行 脚本,这会起作用。但是,如果脚本是从 LaunchAgent bash 脚本启动的,则它没有这样做的权限。

为示例起见,简化了 python 脚本:

with open('/Volumes/nas_1/test/somefile.txt', 'a') as the_file:
                            the_file.write('Hello\n')

Bash LaunchAgent 启动的脚本位于 /Applications:

#!/bin/bash

#Start test script only if it is not running
if [ "$(ps -ef | grep -v grep | grep python_test.py | wc -l)" -le 0 ]
then

echo "Python Test Starting"
/Users/admin-user/.venvs/test/bin/python /Users/admin-user/projects/test/scripts/python_test.py

else
 echo "Python Test Already Running"
fi

plist 位于 ~/Library/LaunchAgents:

<?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>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>
    <key>Label</key>
    <string>com.test.agent</string>
    <key>Program</key>
    <string>/Applications/runTest.sh</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/runTest.stdout</string>
    <key>StandardErrorPath</key>
    <string>/tmp/runTest.stderr</string>
  </dict>
</plist>

错误:

PermissionError: [Errno 1] Operation not permitted: '/Volumes/nas_1/test/somefile.txt'

我在调试时已授予 /Volumes/nas_1/test 777 权限,但没有帮助。我应该将 bash 和/或 python 脚本移到其他地方吗?

我 运行 遇到了类似的问题。升级到 Catalina 后,我的 bash 通过 rsync 将文件复制到外部驱动器的脚本无法 运行。

以下是我为使其再次工作所做的工作:

  • System Preferences/Security & Privacy/Full Disk Access
  • 中添加/bin/bash
  • 将 bash 脚本中的 shebang 替换为 /bin/bash(原为 /usr/bin/env bash
  • 从我的 plist 中删除 StandardErrorPathStandardOutPath 条目(写入日志文件被拒绝,因为它是作为另一个进程产生的)

之后它又开始工作了,不过也许有更好的解决方案。