Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a node.js script with launchd

I'm trying to run a web scraper I made with node.js. node app.js runs the file successfully, but I cannot get launchd to run the script. Here is my plist file:

<?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.username.kijijiscraper</string>

  <key>Program</key>
  <string>/usr/local/bin/node</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/username/documents/nodeprojects/scraper/app.js</string>
  </array>

  <key>StandardOutPath</key>
  <string>/Users/username/documents/nodeprojects/scraper/launchdOutput.log</string>
  <key>StandardErrorPath</key>
  <string>/Users/username/documents/nodeprojects/scraper/launchdErrors.log</string>

</dict>
</plist>

I then load it with launchctl load /Users/username/library/launchagents/com.username.kijijiscraper.plist which is where the plist file is saved.

Eventually it will be run on an interval, but for now I'm trying to test it with launchctl start com.username.kijijiscraper.

The expected behaviour - receiving an email based on the result of the scrape - doesn't happen, and no errors are in the log file. Again, the webscraper works from the terminal.

like image 759
Martin Avatar asked Feb 13 '26 21:02

Martin


1 Answers

You need start the service by:

launchctl start com.username.kijijiscraper

after load plist, or just append this:

<key>RunAtLoad</key>
<true/>

and this if you need:

<key>KeepAlive</key>
<true/>
like image 164
reker Avatar answered Feb 15 '26 10:02

reker