我有一些这样的服务,我希望在文件修改后立即运行。
<?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>test</string>
<key>ProgramArguments</key>
<array>
<string>say</string>
<string>a</string>
</array>
<key>WatchPaths</key>
<array>
<string>/Users/username/Desktop/</string>
</array>
</dict>
</plist>
即使ThrottleInterval设置为1或0,它们最多也只能每10秒运行一次。
9/9/12 4:57:05.457 PM com.apple.launchd.peruser.501[138]: (test) Throttling respawn: Will start in 7 seconds
9/9/12 4:57:09.541 PM com.apple.launchd.peruser.501[138]: (test) Throttling respawn: Will start in 3 seconds
man launchd.plist
只说默认情况下程序运行不超过每10秒一次,但没有提到ThrottleInterval不能设置为低于该值。
ThrottleInterval <integer>
This key lets one override the default throttling policy imposed on jobs by launchd.
The value is in seconds, and by default, jobs will not be spawned more than once
every 10 seconds. The principle behind this is that jobs should linger around just
in case they are needed again in the near future. This not only reduces the latency
of responses, but it encourages developers to amortize the cost of program invoca-
tion.
您可以使程序或脚本运行10秒钟,并每秒监视更改:
#!/bin/bash
start=$(date +%s)
prev=
until (( $(date +%s) >= $start + 10 )); do
new=$(stat -f %m ~/Desktop/)
[[ $prev != $new ]] && say a
prev=$new
sleep 1
done
还是与Ruby中的相同:
#!/usr/bin/env ruby
start = Time.now
prev = nil
until Time.now >= start + 10
current = File.mtime("#{ENV['HOME']}/Desktop/")
`say a` if current != prev
prev = current
sleep 1
end
但是,有什么方法可以绕过或减少时间限制吗?它也适用于文件夹操作。