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)
-----------
如何获得结果
答案 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_start
,on_picture_save
等之一)将其配置为在发生某些事件时运行脚本。