brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a LaunchAgent with StartCalendarInterval is cron that catches up after sleep

macOS
~/tips/tip-macos-launchagent-startcalendarinterval

cat ~/tips/tip-macos-launchagent-startcalendarinterval.md

Schedule your own script with launchd so a missed run happens when the Mac wakes.

Cron still works on a Mac, but a MacBook is asleep half the time, and cron simply skips a run it missed. launchd, the macOS service manager, has a better answer.

Write a small plist in ~/Library/LaunchAgents, here com.example.cleanup.plist, that runs a script every day at 09:00:

<?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.example.cleanup</string>
  <key>ProgramArguments</key><array><string>/Users/you/bin/cleanup.sh</string></array>
  <key>StartCalendarInterval</key><dict>
    <key>Hour</key><integer>9</integer>
    <key>Minute</key><integer>0</integer>
  </dict>
</dict></plist>

Check it and load it into your own session:

plutil -lint ~/Library/LaunchAgents/com.example.cleanup.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.cleanup.plist

The launchd.plist man page is clear about the difference with cron: when the Mac was asleep at 09:00, launchd starts the job the next time it wakes. Several missed runs are combined into one. Keys you leave out work as wildcards, like a star in crontab.

That catch-up is the reason I’d pick launchd over cron on a laptop. To stop it, use launchctl bootout with the same target and path, then delete the file. Write the full path to the script, because a LaunchAgent doesn’t load your shell profile, so your usual PATH isn’t there.

Further reading: launchd.plist.5 man page, launchctl.1 man page

More macOS tips System Settings > Accessibility > Hover Text > When using the pointer A Smart Folder in Notes collects notes by tag, checklist or date Type # in a Mac note to tag it, then filter by tag in the sidebar tmutil isexcluded ~/Projects/app/node_modules Crop an image in Preview by dragging a frame and clicking Crop All macOS tips (99) All topics

Link