【问题标题】:Python subprocess doesn't work when ran by launchd由 launchd 运行时 Python 子进程不起作用
【发布时间】:2014-04-13 23:41:16
【问题描述】:

我有一个 Python 脚本,它使用 subprocess 来运行改变我的桌面背景的 AppleScript。 launchd 会定期调用 bash 脚本来运行 Python 脚本。

当我从命令行运行 bash 脚本时,它工作正常,但问题是当 launchd 以完全相同的方式调用 bash 脚本时,AppleScript 似乎不起作用(我已经调试了它直到这个点,并且子流程调用之前的所有内容都可以正常工作)。我的猜测是它的权限问题,但 launchd 似乎正在像我一样运行脚本。我什至尝试过让它以 root 身份运行,但它仍然无法正常工作。作为守护进程和用户管理员 (/Library/LaunchAgents) 运行它也不起作用。

这是我的 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>Disabled</key>
        <false/>
        <key>Label</key>
        <string>com.foo.background.change</string>
        <key>Nice</key>
        <integer>-15</integer>
        <key>ProgramArguments</key>
        <array>
                <string>/bin/bash</string>
                <string>-c</string>
                <string>/Users/scott/Projects/Background/env/bin/Background/change_background.sh</string>
        </array>
        <key>ServiceDescription</key>
        <string>Runs a script to change the desktop background image.</string>
        <key>ServiceIPC</key>
        <false/>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>UserName</key>
        <string>scott</string>
</dict>
</plist>

感谢所有帮助!

编辑: 感谢您的回复!这是python脚本。 set_desktop_background 是从另一个确定使用哪个图像的方法调用的。始终使用完整的图像路径。

import subprocess

SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""

def set_desktop_background(filepath):
    subprocess.Popen(SCRIPT%filepath, shell=True)

【问题讨论】:

  • 你能给我们看一下 Python 脚本吗?或者至少部分使用subprocess 模块?
  • 听起来像是 PATH 问题。您是否在 subprocess 命令中指定了可执行文件的完整路径?
  • 是的,请查看我的编辑。谢谢!

标签: python launchd


【解决方案1】:

终于明白了。在我编写此脚本时,我在 OSX 的系统偏好设置中启用了“更改图片:每天”功能(我想看看是否能找到用于此过程的 plist)。一旦我最终意识到这一点并禁用它,launchd 就能够通过我的脚本更改背景。

我仍然不确定为什么会发生这种情况,因为我可以在手动运行脚本时更改背景,但是哦。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2016-06-11
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多