TransWikia.com

How do I set a task to run at specific time

Super User Asked on December 27, 2021

This question is partially answered here: https://stackoverflow.com/q/132955/6260775

I was looking for a solution to the particular situation where event runs with an interval but at fixed times such as 10:00, 10:30, 11:00, 11:30… and so on, and wanted to avoid scheduling each single event in CalendarEvents. It might get quite messy when we want to run, let’s say, every 10 minutes.

When using StartInterval with a value e. g. 1800 the event will shift in time, depending how long the task takes to complete. So at some point we won’t be able to tell when the task will run exactly.

One Answer

StartCalendarInterval allows to define run schedules similar to cron.

<?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.example.exampled</string>

     <key>Program</key>
     <string>/path/tp/exampled</string>

     <key>ProgramArguments</key>
     <array>
          <string>exampled</string>
          <string>argv1</string>
          <string>argv2</string>
     </array>

     <key>StartCalendarInterval</key>
     <array>

         <dict>
             <key>Minute</key>
             <integer>0</integer>
         </dict>

         <dict>
             <key>Minute</key>
             <integer>30</integer>
         </dict>

     </array>
</dict>
</plist>

The task above runs whenever the minute part of the current time is 0 or 30.

To quote from the man page (man launchd.plist):

StartCalendarInterval <dictionary of integers or array of dictionaries of integers>

This optional key causes the job to be started every calendar interval as specified. Missing arguments are considered to be wildcard. The semantics are similar to crontab(5) in how firing dates are specified. Multiple dictionaries may be specified in an array to schedule multiple calendar intervals.

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.

Note that StartInterval and StartCalendarInterval are not aware of each other. They are evaluated completely independently by the system.

     Minute <integer>
     The minute (0-59) on which this job will be run.

     Hour <integer>
     The hour (0-23) on which this job will be run.

     Day <integer>
     The day of the month (1-31) on which this job will be run.

     Weekday <integer>
     The weekday on which this job will be run (0 and 7 are Sunday). 
     If both Day and Weekday are specificed, then the job will be 
     started if either one matches the current date.

     Month <integer>
     The month (1-12) on which this job will be run.

Answered by Mecki on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP