6

I'm trying to run a node script every hour on my Mac (OSX 10.8.2 Mountain Lion). According to high ranking search results, the best way to do this is to add a launchd launch agent.

For some reason it doesn't seem to work.

I wrote the following launch agent file and saved it as ~/Library/LaunchAgents/agenttest.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>KeepAlive</key>
        <dict>
            <key>SuccessfulExit</key>
            <false/>
        </dict>
        <key>Label</key>
        <string>protocol</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/node</string>
            <string>/Users/snorpey/PATH/TO/file.js</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>3600</integer>
    </dict>
</plist>

I started the launch agent by running launchctl load ~/Library/LaunchAgents/agenttest.plist in the terminal.

I also made the script file executable with sudo chmod -x /Users/snorpey/PATH/TO/file.js and updated the file permissions with sudo chmod 777 /Users/snorpey/PATH/TO/file.js.

When I run /usr/local/bin/node /Users/snorpey/PATH/TO/file.js in the terminal, the script works fine.

However, it doesn't seem to work when it's run by launchd. I get the following messages in /var/log/system.log:

Jan 15 22:32:46 snorpey com.apple.launchd.peruser.501[595] (agenttest[21625]): Exited with code: 1
Jan 15 22:32:46 snorpey com.apple.launchd.peruser.501[595] (agenttest): Throttling respawn: Will start in 10 seconds

Why does it fail to execute the script?

3 Answers 3

5

I was bumping into this for a while (getting 127 errors). It turns out you need something like this in your plist:

<key>EnvironmentVariables</key>
<dict>
  <key>PATH</key>
  <string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>

launchd seems to execute things with a minimal path that doesn't include /usr/local/bin, and node seems to need this on your PATH to function properly.

1

Turns out I have to run this job as root for whatever reason.

I moved the file to /Library/LaunchAgents/agenttest.plist.

I also updated file ownership with sudo chown root /Library/LaunchAgents/agenttest.plist.

And the file permissions with sudo chmod 644 /Library/LaunchAgents/agenttest.plist.

To run the job as root: sudo launchctl load /Library/LaunchAgents/agenttest.plist

2
  • 1
    If it wouldn't have needed to run as root, maybe the error was fixed just by reloading the plist. You normally have to unload and load plists to apply changes.
    – Lri
    Jan 16, 2013 at 16:32
  • I still see an error 127 when I run launchd list :(
    – Chet
    Apr 29, 2018 at 23:11
1

Another way to set our own PATH by executing command like this bash -c "PATH=xxx && run_script"

<?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>ProgramArguments</key>
        <array>
            <string>bash</string>
            <string>-c</string>
            <string>PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin && node /Users/snorpey/PATH/TO/file.js</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .