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?)



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)


2 Answers 2


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.

  • After some effort playing with launchd, I have reverted to cron. There were some issues getting cron to work on Mohave. I used advice from here [stackoverflow.com/questions/32781884/…
    – pheon
    Commented Jan 9, 2019 at 16:42

I had a similar issue and addressed it by using a shell conditional evaluation:

    <string>minute=$(date +"%M");   if [[ $minute = 00 || $minute = 30 ]]; then afplay -v 2 /System/Library/Sounds/Submarine.aiff; fi</string>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .