1

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?

2

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.

| improve this answer | |
  • This does help because in the output file there is 'hello'. I thought it would show just in the terminal. Thanks. – jinglei Jun 27 '16 at 14:10
0

Then number you see in launchctl list is the status code the daemon-process emitted when it exited.

So, the good news is that the process ran.

Status 78 is a general exit code, so the bad news is that it doesn't tell us much.

Not helping your exact problem, but perhaps spills some light over what's going on.

| improve this answer | |
0

This error for me was being caused by some permission issue caused by my specifying a StandardOutPath and StandardErrPath whose (same) final folder didn't exist. Loading the plist did create the folder, but without giving my user any permissions to it. I chmod'ed, but still got the 78 error code and nothing was being logged to either log file. I didn't see any errors in Console.app either.

I solved the problem by unloading the plist, deleting the automatically-created folder, recreating the folder manually, and then reloading the plist. The daemon is running correctly now, and logs are being written as expected.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.