Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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

share|improve this answer
    
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. – user495470 Jan 16 '13 at 16:32

You must log in to answer this question.

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