macOS – Why Does This LaunchAgent Plist Not Run?

launchdmacos

I have the following plist file as ~/Library/LaunchAgents/wealthychef.obsidian-daily.plist. I'm wondering why it does not execute when loaded. I feel like it should run every minute from 18:00 to 18:59 as written, and if the computer is asleep at those times, then it should run when the computer wakes up… yet it does not. It does not run at all, even though I am always logged in on the machine. Can someone help me understand what I'm doing wrong? Thank you.

<?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>wealthychef.obsidian-daily</string>

    <key>Program</key>
    <string>/Users/rcook/bin/obsidian-daily.sh</string>

    <key>StandardErrorPath</key>
    <string>/Users/rcook/bin/log/obsidian-daily.err</string>

    <key>StandardOutPath</key>
    <string>/Users/rcook/bin/log/obsidian-daily.out</string>
    <key>StartCalendarInterval</key>
    <dict>
        <!--Missing arguments are considered wildcards-->
        <key>Hour</key>
        <integer>18</integer>
    </dict>
</dict>
</plist>

I have validated it and loaded it:

rcook: launchctl unload ~/Library/LaunchAgents/wealthychef.obsidian-daily.plist
rcook: launchctl load ~/Library/LaunchAgents/wealthychef.obsidian-daily.plist
rcook: plutil ~/Library/LaunchAgents/wealthychef.obsidian-daily.plist
/Users/rcook/Library/LaunchAgents/wealthychef.obsidian-daily.plist: OK

The script /Users/rcook/bin/obsidian-daily.sh exists and is executable and works as expected when launched from the Terminal. Here are its contents. The command in the script worked fine when executed from a cron job, but won't run if the Mac is asleep, so I want to use launchd, which should run when the Mac wakes up, but does not. In fact, it doesn't run at all, awake or asleep. The funky URL I'm opening is to be launched by Obsidian.md app.

#!//usr/bin/env bash
echo "It is running on $(date)" > ${HOME}/script-turd.txt
open 'obsidian://advanced-uri?vault=wealthyvault&daily=true&mode=prepend' 

Best Answer

launchd does not run jobs when the Mac is asleep. If you use StartCalendarInterval it will run it the next time the Mac wakes up in a normal user-visible manner (that is to say, "dark wakes" a.k.a. "power nap" wakes do not count).

If the Mac was off (not just asleep) when the StartCalendarInterval hit, the missed instance will not automatically be run on next boot; it will be skipped until the next StartCalendarInterval is hit.

See the "Daemons and Services Programming Guide > Scheduling Timed Jobs > Effects of Sleeping and Powering Off"

Also note that LaunchAgents only run for the current logged-in user. I believe this only means GUI logins (WindowServer / LoginWindow logins), not, for example, SSH logins. If you are not logged in, your LaunchAgents will not run, even if the Mac is awake. In contrast, LaunchDaemons run even when no user is logged in (as long as the Mac is awake), but LaunchDaemons are considered systemwide and not per-user, so they aren't allowed as much access to any given user's environment / context / data / files.

Related Question