macOS Catalina
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.
Simplified python script for example's sake:
with open('/Volumes/nas_1/test/somefile.txt', 'a') as the_file:
the_file.write('Hello\n')
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
plist located in ~/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>
Error:
PermissionError: [Errno 1] Operation not permitted: '/Volumes/nas_1/test/somefile.txt'
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?
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
in System Preferences
/ Security & Privacy
/ Full Disk Access
/bin/bash
(was /usr/bin/env bash
)StandardErrorPath
and StandardOutPath
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With