【发布时间】:2016-05-03 17:02:14
【问题描述】:
编辑:我将其范围缩小到 python 代码中 Notifier.notify('Something') 的问题。当从 launchd 启动 python 脚本时,这不会产生预期的行为。我的其他 python 脚本工作正常。 /编辑
OSX 10.11.4:当目录中的某些内容发生更改(例如,有人添加了文件)时,我正在尝试使用 launchd 来运行 python 3.5 脚本。我使用以下 plist 文件(放置在 ~/Library/LaunchAgents 中并使用 launchctl 加载),它似乎可以使用 shell 脚本
<?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.test.notifyme</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/notifyme.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/path/to/NotifyMeDir</string>
</array>
</dict>
</plist>
这是shell脚本:
#!/bin/bash
# notifyme.sh
osascript -e "tell application \"System Events\" to display notification \"Something\""
但是,当我将 plist 文件更改为:
<key>ProgramArguments</key>
<array>
<string>/path/to/notifyme.py</string>
</array>
调用下面的python程序
#!/path/to/python3.5
# notifyme.py
from pync import Notifier
Notifier.notify('Something')
当我更改 NotifyMeDir 目录中的文件时,我不再获得预期的输出。
/var/log/system.log 在尝试使用 launchd 启动 .py 文件时给出以下信息:
... com.apple.xpc.launchd[1] (com.test.notifyme): Service only ran for 4 seconds. Pushing respawn out by 6 seconds.
所以看起来 launchd 在识别目录更改时工作正常 - 它只是没有执行 python 脚本。
我有一个解决方法,涉及从 shell 脚本调用 python 程序。但是,这仅在我调用 python 程序后使用“osascript”执行 shell 命令时才有效。我在 shell 脚本和 python 脚本上都适当地调用了“cdmod u+x ...”,当它们在launchd之外单独调用时它们都可以工作。当与“WatchPaths”以外的东西一起使用时(例如每 15 秒运行一次),它也能正常工作。
这里是解决方法:
#!/bin/bash
/path/to/python /path/to/notifyme.py
osascript -e "tell application \"Finder\" to activate"
如您所见,这似乎没有任何关系 我难住了。我想在不需要调用 shell 脚本来调用 python 脚本的情况下运行这个。
【问题讨论】: