Skip to content

Python exec linux process

wordpress meta

title: 'Python Exec Linux Process'
date: '2012-11-06T01:57:41-06:00'
status: publish
permalink: /python-exec-linux-process
author: admin
excerpt: ''
type: post
id: 46
category:
    - Python
tag: []
post_format: []

While I am writing a curses based recording application in Python I thought it a good idea to jot down what I did to call a process and get the pid, then run for a set number of minutes and then kill the pid.

def doit_func():
output = subprocess.check_output(["/usr/bin/v4l2-ctl","--device=/dev/" + cfg_dict['source'],"--set-ctrl=video_bitrate="
 +  cfg_dict['bitrate']])

    tsStream = open(cfg_dict['target'],"wb")

    catProc = subprocess.Popen(["/bin/cat","/dev/video1","&"], stdout=tsStream)
    pid = str(catProc.pid) 

    start_time = time.time()
    elapsed_mins = 0

    while elapsed_mins != mins:
      counter = counter + 1
      elapsed_mins = int(time.time() - start_time) / 60
      draw_dict("recording for " + str(elapsed_mins) + " mins")

    output = subprocess.check_output(["/bin/kill","-9",pid])