2013-01-01 62 views
6

這裏有一個奇怪的問題。我有〜/庫/ LaunchAgents/com.me.helloworld.plist具有以下內容:launchd:WatchPaths不會觸發簡單的「hello world」腳本(OS X 10.8)

<?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.me.helloworld</string> 
     <key>ProgramArguments</key> 
     <array> 
       <string>/Users/me/temp/test.sh</string> 
     </array> 
     <key>WatchPaths</key> 
     <array> 
       <string>/Users/me/temp/Journal.txt</string> 
     </array> 
</dict> 
</plist> 

的想法是,如果〜/溫度/ Journal.txt被修改,它應該執行〜/溫度/測試.SH。 test.sh有以下內容:

#!/bin/bash 
echo "hello world from test.sh" >> itchanged.txt 

所以,如果我改變Journal.txt,我應該得到一個名爲itchanged.txt文件。當我做到以下幾點:

$ launchctl unload ~/Library/LaunchAgents/com.me.helloworld.plist 
$ launchctl load ~/Library/LaunchAgents/com.me.helloworld.plist 
$ touch Journal.txt 

做一個LS表明test.txt的不創建。但是,如果我手動執行./test.sh,則會創建它changed.txt。所以這個問題似乎是在認識到Journal.txt被更改並在發生這種情況時執行腳本的地方。

我使用的是OS X Mountain Lion。有任何想法嗎?

回答

7

您的腳本沒有爲itchanged.txt指定路徑;由於啓動進程啓動進程的默認工作目錄是/,並且您的帳戶可能無權在其中創建文件,因此不會創建itchanged.txt文件。您應該在腳本中指定路徑,或將WorkingDirectory鍵添加到.plist以更改默認值。

+0

這工作!非常感謝,戈登。 – user1940620