2

I have a very simple Launch Daemon, com.daily.vocab.plist, which simply runs a basic script. It is supposed to run every day at a certain time (using StartCalendarInterval), but I can't get that to happen. Many posts online are saying it runs a minute or two off, but for me it just never runs.

I have been setting the run time to be 5 minutes in the future while I am testing, and now have it set to run at the 0th second of every minute, just so I can get it to work. Once it's working, I will set it to my desired daily run time.

As advised in this tutorial, it is saved in /Library/LaunchDaemons. I loaded it manually with launchctl load /Library/LaunchDaemons/com.daily.vocab.plist. I do in fact see it has loaded when I run launchctl list. When I manually tell it to run with launchctl start com.daily.vocab, it works immediately as expected. The only issue is that the StartCalendarInterval seems to be ignored. I am on Mavericks on my Macbook.

Here is the file: com.daily.vocab.plist

<?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.daily.vocab</string>
    <key>Program</key>
      <string>/Users/MGS/developer/projects/vocab-sms/scripts/DEvocab.sh</string>
    <key>StartCalendarInterval</key>
      <dict>
        <key>Second</key>
            <integer>0</integer>
      </dict>
  </dict>
</plist>

Ideally, I believe I want it to run at 7:15am every day making the StartCalendarInterval section be instead:

<key>StartCalendarInterval</key>
    <dict>
      <key>Hour</key>
        <integer>7</integer>
      <key>Minute</key>
        <integer>15</integer>
    </dict>

If you could even share an example of a LaunchDaemon you got to work using StartCalendarInterval and every step you made to make it work (e.g. running launchctl load x.x.x.plist), that would be really helpful!

Solution: This local daemon should actually be a user agent, so it needed to be saved in and loaded from the ~/Library/LaunchAgents directory. From there, I also needed to unload the agent and reload the agent.

NOTE: Daemons/Agents do not automatically update in launchd until you unload/load. You can't just save the updated .plist and expect it to work!

mgs
  • 57
  • 7

2 Answers2

0

launchd plists saved in /Library/... are daemons / agents in the local domain and have to be loaded with sudo. Without sudo you're loading the daemon in the user domain which doesn't match the actual path.

Since you are calling a script in the user domain anyway, save the script in ~/Library/LaunchAgents (it's not a daemon). The plist syntax is supposed to be correct.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Hmm. Still not working. The file com.daily.vocab.plist is now saved in ~/Library/LaunchAgents and I have loaded it (without sudo) into launchd. launchctl list confirms this. But it still is not running. Any special permissions I need to add? Does the shell script the agent calls need to have special permissions? – mgs May 07 '16 at 16:54
  • I just changed the agent to use StartInterval instead of StartCalendarInterval and that worked exactly as expected. Any ideas on why StartCalendarInterval isn't working? – mgs May 07 '16 at 17:45
  • No, there's nothing wrong with the `StartCalendarInterval` syntax and further permissions are not needed either. But of course by setting minute and hour you have to wait 24 hours for the next occurrence ;-) – vadian May 07 '16 at 17:50
  • 1
    Ok, I figured it out. I hadn't unloaded and reloaded the agent since switching from the original /Library/LaunchDaemons directory. Once I moved it to ~/Library/LaunchAgents, and unloaded and reloaded the .plist, it worked. Thanks so much @Vadian! – mgs May 07 '16 at 17:56
  • 1
    I recommend checking out the util "Launch Control" for creating and managing launchd jobs. The confusion around the right directory and whether a job is loaded or not becomes immediately clear with Launch Control. Cheers! (not mine, I just love it) – Chris Oct 19 '21 at 17:57
0

will have to change the permission for the bash file

chmod u+v /Users/MGS/developer/projects/vocab-sms/scripts/DEvocab.sh
J11
  • 455
  • 4
  • 8