#!/usr/bin/python
import re
import time
import shlex
from subprocess import Popen, PIPE
def count_motion(*args, **kwargs):
query = "motion"
ps_process = Popen(["ps", "aux"], stdout=PIPE)
grep1_process = Popen(["grep", "-v", "grep"], stdin=ps_process.stdout, stdout=PIPE)
grep2_process = Popen(["grep", query], stdin=grep1_process.stdout, stdout=PIPE)
ps_process.stdout.close() # Allow ps_process to receive a SIGPIPE if grep1_process exits.
grep1_process.stdout.close() # Allow ps_process to receive a SIGPIPE if grep2_process exits.
output = grep2_process.communicate()[0]
lines = output.split('\n')
filtered = filter(lambda x: not re.match(r'^\s*$', x), lines)
howmany = len(filtered)
print howmany
return howmany
while True:
if count_motion() < 1:
ps_process = Popen(["sudo", "motion"], stdout=PIPE)
ps_process.stdout.close() # Allow ps_process to receive a SIGPIPE if grep1_process exits.
print 'here we go'
print 'sleep'
time.sleep(1)
# usage:
# python observer.py &