Setting Up A recurring task on MacOS

Traditionally, on *nix systems, cron (or one of its derivatives) is used. It’s possible to use cron on the Mac, but it’s deprecated. launchd is the preferred method. The set up is a little more complex because it’s a bit of XML code, but it’s very straightforward.

I set up dynamic DNS for a remote server in my domain, and wanted to have it update its DNS record once a day at 7am. You create a file in /Users/<yourloginname>/Library/LaunchAgents with the PLIST naming scheme: com.technomancer.updatedns.plist and put in code of the form:

<?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.technomancer.updatedns</string>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/sgarrett/bin/updatedns</string>
  </array>

  <key>Nice</key>
  <integer>1</integer>

  <key>StartCalendarInterval</key>
  <dict>
  <key>Hour</key>
  <integer>7</integer>
  </dict>

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

  <key>StandardErrorPath</key>
  <string>/Users/sgarrett/logs/updatedns.err</string>

  <key>StandardOutPath</key>
  <string>/Users/sgarrett/logs/updatedns.log</string>
</dict>
</plist> 

After you create this, to load it into launchd, run the command:

launchctl load com.technomancer.updatedns.plist 

To stop it, just use unload instead of load.

NOTE! You cannot run the launchctl commands correctly unless you are logged in to the GUI. They will fail otherwise.

Leave a Comment