使用sudo motion运行python脚本无法获取输出

时间:2020-09-28 10:24:19

标签: python python-3.x shell raspberry-pi

import subprocess

p = subprocess.Popen(['sudo motion'],
        shell = True,
          stdout = subprocess.PIPE,
          stdin = subprocess.PIPE)
p.wait()

stdout, stderr = p.communicate()

out = stdout.decode('utf-8')

print('-----------', out)

并且我使用“ python3 sh2.py”运行上述脚本,但得到以下结果

[22636168:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[22636168:motion] [NTC] [ALL] motion_startup: Motion 4.1.1 Started
[22636168:motion] [NTC] [ALL] motion_startup: Logging to file (/var/log/motion/motion.log)
-----------

如何获得结果

2 个答案:

答案 0 :(得分:1)

如果您想通过p.communicate()捕获stderr,则需要在stderr调用中将subprocess.PIPE设置为Popen(相反,这看起来不像您正在使用stdin):

p = subprocess.Popen(['sudo motion'],
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)

答案 1 :(得分:1)

默认情况下,motion作为守护程序启动,在后台运行。

根据正在执行的操作,您可能希望使用on_配置设置(on_event_starton_picture_save等之一)将其配置为在发生某些事件时运行脚本。

相关问题