使用python子进程将OSX plist加载到LaunchDaemons

时间:2013-08-08 04:40:26

标签: python plist subprocess launchd

我正在尝试使用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 list

“ - 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 ,但没有运气。对此有何想法或想法?

1 个答案:

答案 0 :(得分: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)