'Monitor and return the output of a command.
Set self.waiting to False if command has completed.
timeoutms: timeout in ms or None to wait indefinitely
findPid: look for PID from mnexec -p'

def monitor(self, timeoutms=None, findPid=True):

    ready = self.waitReadable(timeoutms)
    if (not ready):
        return ''
    data = self.read(1024)
    pidre = '\\[\\d+\\]    \\d+\\r\\n'
    marker = (chr(1) + '\\d+\\r\\n')
    if (findPid and (chr(1) in data)):
        if re.findall(pidre, data):
            data = re.sub(pidre, '', data)
        while (not re.findall(marker, data)):
            data += self.read(1024)
        markers = re.findall(marker, data)
        if markers:
            self.lastPid = int(markers[0][1:])
            data = re.sub(marker, '', data)
    if ((len(data) > 0) and (data[(-1)] == chr(127))):
        self.waiting = False
        data = data[:(-1)]
    elif (chr(127) in data):
        self.waiting = False
        data = data.replace(chr(127), '')
    return data
