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?