0

I've been trying to run a python script with a .plist file when my mac starts. I put the file into randomuser/library/launchagents and at runtime I always get an error saying that it can't open the python file and the operation is not permitted. What can I do to get the script running as intended when my mac boots up? I've looked at a number of forum and stack overflow posts and followed people's advice but I'm always met with the same 'not permitted' errors. I've also tried running a .sh shell but it leads to the same result.

The error I receive:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/randomuser/Documents/Myprogram/startupscript.py': [Errno 1] Operation not permitted

my .plist file:

<?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>Label</key>
  <string>com.randomuser.startupscript</string>    
  <key>Program</key>
<string>/Users/randomuser/Documents/Myprogram/startupscript.py</string>  
  <key>RunAtLoad</key>
  <true/>    
  <key>StandardErrorPath</key>
<string>/Users/randomuser/Documents/Myprogram/log/com.username.scriptname.err</string>    
  <key>StandardOutPath</key>
<string>/Users/randomuser/Documents/Myprogram/com.username.scriptname.out</string>
</dict>
</plist>

My python file:

#!/usr/bin/env python
import os
import time

while True:
  os.system('echo " foo" >> /Users/randomuser/Documents/Myprogram/log/foostore.txt')
  time.sleep(1)

It finds the file but can't run it. I've also converted the file into an executable so if I do ./startupscript.py in the terminal then it will run fine.

  • The error message quite clearly indicates a permissions problem. What are the permissions on the file? – tripleee Dec 09 '21 at 10:21
  • @tripleee when i ran `ls -l` next to the file it said `-rwxr-xr-x`. Are these permissions correct? – JuniorWebDev28 Dec 09 '21 at 10:25
  • They look correct as such, but the error message suggests that there is something more going on. How are you running the `.plist` definition? If it is run by a user other than yourself, you need to make sure that user has execute permission on every directory along the way. Probably [edit] your question to clarify these things. – tripleee Dec 09 '21 at 10:28
  • 1
    Recent versions of macOS restrict access to "private" areas, like user Documents folders, independently of regular file permissions (see ["What and how does macOS Mojave implement to restrict applications access to personal data?"](https://apple.stackexchange.com/questions/332673) at Ask Different). I'd move the script somewhere public. Alternately, you can grant access to the script, but it's messy; see ["How to run a LaunchAgent that runs a script which causes failures because of System Integrity Protection"](https://apple.stackexchange.com/questions/338213). – Gordon Davisson Dec 09 '21 at 10:56
  • 1
    Possible duplicates here on SO: ["LaunchAgent doesn't run shell script"](https://stackoverflow.com/questions/60323908/launchagent-doesnt-run-shell-script) and ["LaunchAgent script can't write to external drive"](https://stackoverflow.com/questions/58677123/launchagent-script-cant-write-to-external-drive). – Gordon Davisson Dec 09 '21 at 10:57

0 Answers0