launchd: Python Notifier.notify 没有产生预期的输出

launchd: Python Notifier.notify not producing expected output

编辑:我将其缩小为 python 代码中 Notifier.notify('Something') 的问题。当 python 脚本从 launchd 启动时,这不会产生预期的行为。我的其他 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 秒 运行ning)。

解决方法如下:

#!/bin/bash
/path/to/python /path/to/notifyme.py

osascript -e "tell application \"Finder\" to activate"

如你所见,这似乎没有任何关系 我很难过。我想要这个 运行 而无需调用 shell 脚本来调用 python 脚本。

好的 - 我知道了。 Pync.Notifier.notify 正在对 /usr/local/bin/terminal-notifier 进行系统调用。程序的其余部分不会等待它被调用,所以程序在系统调用完成之前就结束了(我猜?)。无论如何,解决方案只是确保 python 脚本在 Notifier.notify 调用之后有足够的内容,以便可以完成对 /usr/local/bin/terminal-notifier 的调用。 time.sleep() 调用在这里是一种无害的解决方法。

解决方案:

#!/path/to/python3.5
# notifyme.py
from pync import Notifier
import time

Notifier.notify('Something')
time.sleep(0.1)

不要使用 time.sleep,只需使用 wait 选项。

根据官方notify docstring

The options 'wait' is a boolean for whether or not we need to wait (block) for the background process to finish

所以,只是 运行 和

pync.notify('message', wait=True)

参考:notify sourcecode