LaunchAgent脚本无法写入外部驱动器 [英] LaunchAgent script can't write to external drive
问题描述
macOS Catalina
macOS Catalina
我有一个python脚本,应该将文件写入外部驱动器.如果我手动运行脚本,则此方法有效.但是,如果该脚本是从LaunchAgent bash脚本开始的,则没有这样做的权限.
I have a python script that should write a file to an external drive. This works if I run the script manually. However, if the script is kicked off from a LaunchAgent bash script, it doesn't have permission to do so.
例如,简化的python脚本:
Simplified python script for example's sake:
with open('/Volumes/nas_1/test/somefile.txt', 'a') as the_file:
the_file.write('Hello\n')
LaunchAgent启动的Bash脚本位于/Applications
:
Bash script that the LaunchAgent kicks off located in /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
位于〜/Library/LaunchAgents
中的
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>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脚本移到其他位置吗?
I've given /Volumes/nas_1/test
777 permissions while debugging and that has not helped. Should I move the bash and or python scripts some where else?
推荐答案
我遇到了类似的问题.我的通过bsync将文件复制到外部驱动器的bash脚本在升级到Catalina后无法运行.
I have run into a similar problem. My bash script which copies files to an external drive via rsync fails to run after upgrading to Catalina.
以下是我使其再次起作用的步骤:
The following is what I did to make it work again:
- 在
系统偏好设置
/安全性&中添加
//bin/bash
隐私完全磁盘访问
- 用
/bin/bash
(以前是/usr/bin/env bash
)将我的shebang替换为bash脚本 - 从我的plist中删除
StandardErrorPath
和StandardOutPath
条目(写入日志文件被拒绝,因为它是作为另一个进程生成的)
- Add
/bin/bash
inSystem Preferences
/Security & Privacy
/Full Disk Access
- Replace my shebang in the bash script with
/bin/bash
(was/usr/bin/env bash
) - Remove
StandardErrorPath
andStandardOutPath
entries from my plist (Writing log files was denied because it was spawned as another process)
此后,它又开始工作了,也许还有更好的解决方案.
After that it starts to work again, maybe there are better solutions though.
这篇关于LaunchAgent脚本无法写入外部驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!