I have a simple launchd script that plays a sound every 30 minutes and it runs out of the user library ~/Library . It doesn't run if my machine is asleep, but does run the last event missed when the machine wakes. I think this is the expected behavior. My question is, how do I stop this happening. If I missed the half-hour event, I don't want it to be run at 20 minutes past the hour.

Here's the core of my script. It calls a Python script. I tried to put a block in the python script to check that I am within a few minutes of each 30 minutes, but that doesn't seem to work on wake. (Is it missing the system clock somehow?)

<key>ProgramArguments</key>
<array>
    <string>/usr/local/bin/python3</string>
    <string>/Users/pheon/Documents/playsound.py</string>
</array>

<key>StartCalendarInterval</key>
<array>
    <dict>
        <key>Minute</key>
        <integer>0</integer>
    <dict>
        <key>Minute</key>
        <integer>30</integer>
    </dict>
</array>

Here is a snippet from the python code which checks the time before playing the sound.

time0 = datetime.datetime.now() if (time0.minute % 30) < 2: subprocess.run(["/Users/pheon/bin/afplay-vol.sh", "1", bell],check=True)

The problem lies with launchd and how it handles jobs that are missed during sleep:

From the launchd.plist man page (man launchd.plist)

Unlike cron which skips job invocations when the computer is asleep,
launchd will start the job the next time the computer wakes up.  If
multiple intervals transpire before the computer is woken, those
events will be coalesced into one event upon wake from sleep.

So, there are a few options to consider...

  • revert to cron. Though deprecated, cron won't run jobs that have been missed

  • check for time of day before playing the sound (this can be done with a "wrapper" script or within the script itself.

  • write a log entry (somewhere). On script launch, parse the log. If a sound was played within the last 30 mins, don't play again.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.