Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaunchAgent script can't write to external drive

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?

like image 757
brewcrazy Avatar asked Nov 03 '19 03:11

brewcrazy


1 Answers

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:

  • Add /bin/bash in System Preferences / Security & Privacy / Full Disk Access
  • Replace my shebang in the bash script with /bin/bash (was /usr/bin/env bash)
  • Remove 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.

like image 146
5t111111 Avatar answered Oct 03 '22 00:10

5t111111