【问题标题】:load OSX plist to LaunchDaemons with python subprocess使用 python 子进程将 OSX plist 加载到 LaunchDaemons
【发布时间】:2013-08-09 16:38:55
【问题描述】:

我正在尝试使用 python 安装 OSX LaunchDaemon,但使用 subprocess.Popen 调用 launchctl 实际上并没有安装该服务。

我在 /Library/LaunchDaemons/ 中有 plist 文件,我可以使用终端很好地加载 plist 文件:

$ launchctl load -w /Library/LaunchDaemons/com.myplist.file.plist

$ launchctl start com.myplist.file

$ launchctl 列表

"- 0 com.myplist.file"

服务通过命令行正确加载和启动,这意味着我的 plist 文件设置正确,但是当我使用 python subprocess.Popen 或任何 python 系统调用命令执行相同的命令时问题就开始了。

            # Load the service
            command = shlex.split("launchctl load -w /Library/LaunchDaemons/com.myplist.file.plist")
            subprocess.Popen(command)
            # Start the service
            command = shlex.split("launchctl start com.myplist.file")
            subprocess.Popen(command)

我也尝试过设置 shell=True 但没有成功。对此有何想法或想法?

【问题讨论】:

    标签: python plist subprocess launchd


    【解决方案1】:

    我想通了!谢谢你的帮助,自我。哦,不客气,自我!

    任何想通过 python 安装 OSX 服务的人都会发现这很有用。

    加载服务

    servicePath = '/Library/LaunchDaemons/com.myplist.file.plist'
    
    launchctlCmd = ['/bin/launchctl', 'load', '-w', servicePath]
    # Execute service load command
    proc = subprocess.Popen(launchctlCmd, shell=False, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    

    启动服务

    serviceName = 'com.myplist.file'
    
    launchctlCmd = ['/bin/launchctl', 'start', serviceName]
    # Execute service start command
    proc = subprocess.Popen(launchctlCmd, shell=False, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-07
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多