【问题标题】:launchd: Python Notifier.notify not producing expected outputlaunchd:Python Notifier.notify 没有产生预期的输出
【发布时间】: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 脚本的情况下运行这个。

【问题讨论】:

    标签: python macos launchd


    【解决方案1】:

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

    根据官方通知文档字符串

    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

    【讨论】:

      【解决方案2】:

      好的 - 我明白了。 Pync.Notifier.notify 正在对 /usr/local/bin/terminal-notifier 进行系统调用。程序的其余部分不会等待这个被调用,所以程序在系统调用完成之前就结束了(我猜?)。无论如何,解决方案只是确保在Notifier.notify 调用之后python 脚本有足够的东西,以便对/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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-13
        • 2020-12-26
        相关资源
        最近更新 更多