Raspberry Pi:从python终止raspistill命令

Raspberry Pi:从python终止raspistill命令,python,linux,subprocess,raspberry-pi,Python,Linux,Subprocess,Raspberry Pi,我正在尝试使用服务器上的PHP文件将一些变量传输到Python脚本中,该脚本将在我的Raspberry Pi上启动raspistill timelapse 到目前为止,我已经设法开始拍照,但我现在想有一个按钮来关闭timelapse-我尝试了许多方法,包括.kill()和.terminate(),但无法使其工作 以下是我当前的python代码: import sys, os, time, datetime import subprocess import signal from time imp

我正在尝试使用服务器上的PHP文件将一些变量传输到Python脚本中,该脚本将在我的Raspberry Pi上启动raspistill timelapse

到目前为止,我已经设法开始拍照,但我现在想有一个按钮来关闭timelapse-我尝试了许多方法,包括.kill()和.terminate(),但无法使其工作

以下是我当前的python代码:

import sys, os, time, datetime
import subprocess
import signal
from time import sleep

tlfreq = int(sys.argv[1])
tltime = int(sys.argv[2])
dir = '/var/www/timelapse/' + sys.argv[3]

if not os.path.exists(dir):
    os.makedirs(dir)

cmd = ('raspistill -t ' + str(tltime) + " -tl " + str(tlfreq) + " -o " + dir + "/photo_%04d.jpg")
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                   shell=True, preexec_fn=os.setsid)
print "Pictures are now being taken every" , tlfreq/1000 , "second/s for a total of", tltime/3600000 , "hours. These are being stored in", dir
也许我需要一个“if variable=1 then kill”命令,然后将变量发送给python

任何帮助都将不胜感激

非常感谢,,
Dan

我建议使用信号库:

您可以使用此代码创建新的python脚本kill\u raspystill.py

import os

os.system("pkill raspistill")

并在按下按钮时调用该脚本。

您可以调用
os.killpg(pro.pid,signal.SIGTERM)
来终止
pro
子进程及其所有子进程。除非以后阅读
pro.stdout
,否则不要使用
stdout=subprocess.PIPE
。注意:它会尝试杀死所有
raspistill
命令。您可以使用
子流程。检查调用(['pkill',raspistill'])
以避免生成shell。