Can't get launchd plist run successfully in Mac OS X

12,253

I don't know what the exit status of 78 means, but I would not expect this script to do anything detectable when run as a launch agent, because its output is not directed anywhere. When you run it from Terminal, its output is attached to that Terminal window, so that's where the "hello" appears. But launchd has no connection to Terminal (or any other relevant output target), so the "hello" gets discarded. If you want to check whether it's running, you can direct its outputs (both standard output and error output) to files by adding these keys to the .plist (and then unloading and reloading it):

<key>StandardOutPath</key>
<string>/Users/yangyy/hello-output.txt</string>
<key>StandardErrorPath</key>
<string>/Users/yangyy/hello-errors.txt</string>

Now, it's also possible that launchd is having some other problem even starting the script. To see if launchd itself is reporting any problems, look in the system log file (tail -f /var/log/system.log or run /Applications/Utilities/Console.app), then try to unload+reload the agent and see if anything relevant appears in the log.

Share:
12,253

Related videos on Youtube

jinglei
Author by

jinglei

Updated on September 18, 2022

Comments

  • jinglei
    jinglei over 1 year

    I’m using Mac OS X 10.10.5 (Yosemite) an learning to use launchd (Launch Daemon) to auto-run some scripts. To make it simple, I decide to use a shell script which only contains a echo command.

    Contents of hello.sh:

    #! /bin/sh
    echo "hello"
    

    Also I’ve run chmod a+x hello.sh to make it executable and when I run the script manually it works fine.

    In ~/Library/LaunchAgents/ I have com.yang.hello.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>Label</key>
            <string>com.yang.hello</string>
            <key>Program</key>
            <string>/Users/yangyy/hello.sh</string>
            <key>RunAtLoad</key>
            <true/>
        </dict>
    </plist>
    

    After I run:

    launchctl load -w ~/Library/LaunchAgents/com.yang.hello.plist
    

    No error message appears and if I run this command:

    launchctl list | grep "com.yang.hello"
    

    The response I get is:

    -   78  com.yang.hello.plist
    

    It seems that positive number indicates the exit code and the program is not actually running.

    Besides, I find something strange when I tried to use launchctl start ~/Library/LaunchAgents/com.yang.hello.plist to run the program, but when I use this command it returns error and no error message.

    What’s wrong?

  • jinglei
    jinglei almost 8 years
    This does help because in the output file there is 'hello'. I thought it would show just in the terminal. Thanks.