Python 脚本在终端中有效,但在 launchd 中无效
Python script works in terminal but not with launchd
我正在尝试 运行 在 OS X 上使用启动的 python 脚本。当我使用以下命令从终端 运行 它时,它起作用了:
$ /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/me/path/to/script.py
这是我启动的 plist 文件,在 ProgramArguments 项下有相同的命令,但它不起作用。
<?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>net.me.my-script</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4</string>
<string>/Users/me/path/to/script.py</string>
</array>
<key>StartInterval</key>
<integer>86400</integer>
</dict>
</plist>
launchd 最初没有工作,所以我尝试 运行 在终端手动启动它,但我收到一个错误并意识到我需要引用我正在读取和写入的文件 python 脚本及其完整路径,所以我修复了它。这使得终端中的脚本 运行 但它没有解决 launchd 中的问题(是的,我已经多次卸载和加载它)。
我 运行 launchctl list
并发现我的 plist 旁边列出了一个 1
退出代码,然后我检查了 /var/log/system.log 并发现错误消息 Service exited with abnormal code: 1
但这是我能找到的所有错误消息。
根据来自 finder 的 'last modified' 日期和内容,它似乎在 python 脚本中打开了一个 xml 文件,但它没有写入任何内容。它似乎也没有在脚本中打开 json 文件。
同样,所有这些东西都可以在终端中手动运行 运行,所以我认为它必须是 plist 或 launchd 问题。
已解决。我能够设置 StandardErrorPath 和 StandardOutPath 键并查看脚本抛出的错误。在操作 xml 文件时,我在 f.write()
和 f.read()
的实例上得到了一些 UnicodeEncodeError
s,但仅当 运行 脚本通过启动时。我用 with io.open('file.xml', 'w', encoding='utf8') as f:
替换了 with open('file.xml', 'w') as f:
,读取实例也是如此,而且它有效!任何人都知道为什么这在我的 IDE 和终端中工作正常但在 launchd 中却没有?全部使用相同的解释器。
我正在尝试 运行 在 OS X 上使用启动的 python 脚本。当我使用以下命令从终端 运行 它时,它起作用了:
$ /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/me/path/to/script.py
这是我启动的 plist 文件,在 ProgramArguments 项下有相同的命令,但它不起作用。
<?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>net.me.my-script</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4</string>
<string>/Users/me/path/to/script.py</string>
</array>
<key>StartInterval</key>
<integer>86400</integer>
</dict>
</plist>
launchd 最初没有工作,所以我尝试 运行 在终端手动启动它,但我收到一个错误并意识到我需要引用我正在读取和写入的文件 python 脚本及其完整路径,所以我修复了它。这使得终端中的脚本 运行 但它没有解决 launchd 中的问题(是的,我已经多次卸载和加载它)。
我 运行 launchctl list
并发现我的 plist 旁边列出了一个 1
退出代码,然后我检查了 /var/log/system.log 并发现错误消息 Service exited with abnormal code: 1
但这是我能找到的所有错误消息。
根据来自 finder 的 'last modified' 日期和内容,它似乎在 python 脚本中打开了一个 xml 文件,但它没有写入任何内容。它似乎也没有在脚本中打开 json 文件。
同样,所有这些东西都可以在终端中手动运行 运行,所以我认为它必须是 plist 或 launchd 问题。
已解决。我能够设置 StandardErrorPath 和 StandardOutPath 键并查看脚本抛出的错误。在操作 xml 文件时,我在 f.write()
和 f.read()
的实例上得到了一些 UnicodeEncodeError
s,但仅当 运行 脚本通过启动时。我用 with io.open('file.xml', 'w', encoding='utf8') as f:
替换了 with open('file.xml', 'w') as f:
,读取实例也是如此,而且它有效!任何人都知道为什么这在我的 IDE 和终端中工作正常但在 launchd 中却没有?全部使用相同的解释器。