launchd

Linux systemd == Macos launchd

Scheduling a script (zettelmerken) to run at specific time

  1. Create a new file com.zettelmerken.dailyreview.plist in ~/Library/LaunchAgents/

  2. Add the following xml to it:

    <?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>Label</key>
      <string>com.zettelmerken.dailyreview</string>
    
      <key>ServiceDescription</key>
      <string>Zettelmerken Daily Review</string>
    
      <key>ProgramArguments</key>
      <array>
        <string>/opt/homebrew/bin/python3</string>
        <string>-m</string>
        <string>zettelmerken</string>
      </array>
    
      <!-- Run on load (at bootup) -->
      <key>RunAtLoad</key>
      <true />
    
      <!-- Run Daily at 00:10 min -->
      <key>StartCalendarInterval</key>
      <dict>
        <key>Hour</key>
        <integer>0</integer>
        <key>Minute</key>
        <integer>10</integer>
      </dict>
    
      <!-- For Debugging
      <key>StandardErrorPath</key>
      <string>/tmp/com.zettelmerken.dailyreview.err</string>
      <key>StandardOutPath</key>
      <string>/tmp/com.zettelmerken.dailyreview.out</string>
      -->
    </dict>
    </plist>
    
  3. Verify plutil ~/Library/LaunchAgents/com.zettelmerken.dailyreview.plist

  4. Load agent launchctl load -w ~/Library/LaunchAgents/com.zettelmerken.dailyreview.plist

  5. Unload agent launchctl unload -w ~/Library/LaunchAgents/com.zettelmerken.dailyreview.plist

  6. Start/Stop (for debugging) launchctl start com.zettelmerken.dailyreview

  7. List all services launchctl list