OS X launchd.plist

隐身守侯 提交于 2021-01-28 14:02:16

问题


Here's my plist file

<?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.homebrew.autoupdate</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/brew_up</string>
  </array>
  <key>StartInterval</key>
  <integer>86400</integer>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/log/brew.log</string>
  <key>StandardOutPath</key>
  <string>/usr/local/var/log/brew.log</string>
</dict>
</plist>

/usr/local/bin/brew_up is just a script which calls brew update, brew upgrade etc

#!/bin/sh
echo `date`: Start updating brew
/usr/local/bin/brew update && /usr/local/bin/brew upgrade && /usr/local/bin/brew cleanup
echo `date`: Finish updating brew

Then I loaded that file by launchctl load ~/Library/LaunchAgents/homebrew.autoupdate.plist

Then I tried launchctl start com.homebrew.autoupdate, it also worked fine.

But the thing is, it won't start every 24 hours, as I'd expected. Actually now over 48 hours has passed, it's still not started even once. What am I missing here?

Then I changed StartInterval to StartCalendarInterval

  <key>StartCalendarInterval</key>
  <dict>
    <key>Hour</key>
    <integer>21</integer>
  </dict>

Then it was started indefinitely, here's the log file:

Thu Aug 7 21:17:20 CST 2014: Finish updating brew
Thu Aug 7 21:17:20 CST 2014: Start updating brew
Already up-to-date.
Thu Aug 7 21:18:14 CST 2014: Finish updating brew
Thu Aug 7 21:18:14 CST 2014: Start updating brew
Already up-to-date.
Thu Aug 7 21:19:39 CST 2014: Finish updating brew
Thu Aug 7 21:19:39 CST 2014: Start updating brew
Already up-to-date.
Thu Aug 7 21:22:28 CST 2014: Finish updating brew
Thu Aug 7 21:22:28 CST 2014: Start updating brew
Already up-to-date.
Thu Aug 7 21:24:46 CST 2014: Finish updating brew

Update: I think that means * 21 * * * as in crontab, so if it was

      <key>StartCalendarInterval</key>
      <dict>
        <key>Hour</key>
        <integer>21</integer>
        <key>Minute</key>
        <integer>0</integer>
      </dict>

it should work.

But still, what's wrong with StartInterval?

BTW, I just tried to write a simple demo to test StartInterval, and it totally worked!

<?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.test.touch</string>
  <key>ProgramArguments</key>
  <array>
    <string>touch</string>
    <string>/tmp/touched.txt</string>
  </array>
  <key>StartInterval</key>
  <integer>60</integer>
</dict>
</plist>

回答1:


For Scheduled Timed Jobs, you should use the key StartCalendarInterval (Reference)

<key>StartCalendarInterval</key>
<dict>
    <key>Hour</key>
    <integer>11</integer>
</dict>



回答2:


There is a note buried somewhere in the launchd developer documentation that cautions that on-demand services need to stay around for some minimum amount of time. (Something like 10 seconds IIRC.) Otherwise, launched will think they failed to launch properly and will try to restart them.

Try adding a sleep 30 to your shell script, and see if that fixes the problem of it continually being restarted.



来源:https://stackoverflow.com/questions/25040207/os-x-launchd-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!