0

Trying to run a user agent:

launchctl load myagent.plist
launchctl start myagent.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>myagent</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/me/folder/startmyagent.sh</string>
        </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Also tried using Program instead of ProgramArguments:

<key>Program</key>
<string>/Users/me/folder/startmyagent.sh</string>

startmyagent.sh:

#!/bin/bash
/Users/me/folder/mybinary
#logger "hello" # bonus issue, this (or echo) doesn't show any output in /var/log/system.log

I see this in /var/log/system.log

Mar 11 20:26:40 My-MacBook-Pro com.apple.xpc.launchd[1] (myagent[26142]): Service exited with abnormal code: 101

I read that it may be something with permissions, I set recursively everything under /Users/me/folder/ to 777, changed owner to root also (it was initially my user):

-rwxrwxrwx  1 root  staff  41651520 Mar 10 15:35 myagent
-rwxrwxrwx@ 1 root  staff        92 Mar 11 20:24 startmyagent.sh
drwxrwxrwx  3 root  staff        96 Mar 11 19:25 log

I'm able to run the binary successfully if I just call the script directly in the terminal:

sh /Users/me/folder/startmyagent.sh

I've no idea how to debug this, can't find anywhere a clear meaning of the 101 error (or solution) and can't get the user agent output to show in the logs or terminal.

User
  • 205
  • Is the script in (or trying to access anything in) a folder that's protected by TCC, like ~/Desktop or ~/Documents? Also, you can add StandardOutPath and StandardErrorPath keys to your .plist to record any informative error messages it prints as it tries to run. Also, 777 permissions are almost always a bad idea. – Gordon Davisson Mar 11 '21 at 20:02
  • @GordonDavisson today I can't run it at all, keeps saying Load failed: 5: Input/output error on launchctl load myagent.plist and I've no idea why. There's nothing in the logs. I added StandardOutPath and StandardErrorPath but there's nothing there as it appears to not even find the file. Its show the same error if I pass an invalid filename. I don't understand why Apple always has to make these kind of things so obscure. Agree about 777, just to test quickly. – User Mar 31 '21 at 20:39
  • @GordonDavisson my files are in a custom subdirectory of ~. I tried moving them to /Library/Application\ Support/mynewdir/ (and adjusted the path in the plist), but can't test since it can't load. Is /Library/Application\ Support/ a valid directory? – User Mar 31 '21 at 20:42
  • /Library/Application Support/ is a valid directory; depending on the context, you might or might not need to escape the space, so that's a possible source of confusion. I'd recommend putting the .plist file in /Library/LaunchAgents/, since that's the standard location, and files there are auto-loaded at system startup (and/or maybe at login, not sure about that). Note that in the ProgramArguments array, you don't need to escape spaces in paths. – Gordon Davisson Mar 31 '21 at 21:47

0 Answers0