Shell.py

From Osvidwiki
Jump to: navigation, search

shell.py

import subprocess, os

"""

Wrapper for a shell command. allows for simple kill

"""

DEBUG = False

class shell (object):

    def __init__ (self, cmd):
        # --device, -D
        self.cmd = cmd
        self.p = None

    def start(self):
        """
        """
        if self.p:
            # print "WARNING: p already defined"
            # self.p.terminate()
            self.stop()
        
        args = []
        args = self.cmd.split()
        # args.append("-c%d" % self.channels)
        DEVNULL = os.open(os.devnull, os.O_RDWR)
        # self.p = subprocess.Popen(args, stderr=DEVNULL)
        self.p = subprocess.Popen(args)

# SYNC
#        if duration != None and self.blocking:
#            os.waitpid(self.p.pid, 0) 

    def stop (self):
        if self.p and self.p.poll() == None:
            self.p.terminate()
        self.p = None

    def is_running(self):
        return self.p and self.p.poll() == None

if __name__ == "__main__":
    print "hello"

    from time import sleep

    motion = shell("motion -cmotion.conf")

    print "Starting..."
    motion.start()
    sleep(10)
    motion.stop()
    print "Stopped"
    sleep(10)
    print "Starting..."
    motion.start()
    sleep(10)
    motion.stop()
    print "Stopped"