【问题标题】:launchd: WatchPaths will not trigger simple "hello world" script (OS X 10.8)launchd:WatchPaths 不会触发简单的“hello world”脚本(OS X 10.8)
【发布时间】:2013-01-01 06:44:41
【问题描述】:

这里有一个奇怪的问题。我有 ~/Library/LaunchAgents/com.me.helloworld.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.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>

思路是,如果修改了~/temp/Journal.txt,就应该执行~/temp/test.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,则会创建 itchanged.txt。因此,问题似乎出在某个地方,即在发生这种情况时识别出 Journal.txt 已更改并执行脚本。

我正在使用 OS X Mountain Lion。有什么想法吗?

【问题讨论】:

    标签: macos bash plist osx-mountain-lion launchd


    【解决方案1】:

    您的脚本没有指定 itchanged.txt 的路径;由于已启动进程的默认工作目录是 /,并且您的帐户可能没有在那里创建文件的权限,因此永远不会创建 itchanged.txt。您应该在脚本中指定一个路径,或者将 WorkingDirectory 键添加到您的 .plist 以更改默认值。

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2017-04-10
      • 1970-01-01
      相关资源
      最近更新 更多