Reputation: 10981
I have the following check_status method:
def check_status(self):
# each of these is a process I want to make sure they are running.
status = {
'pm' : False,
'om' : False,
'dm' : False,
'redis' : False,
}
cmd = "sudo %s/foo status" % config.FOO_TOOLS
output = subprocess.check_output(cmd, shell=True)
print output
for k, v in status.iteritems():
print k
I want to parse output
based on the keys of status
, and if one of them is Not running
I can attempt to restart. I want to do it based on the a regex.
Here is an example when the processes are not running:
[WARN] : current build mode : debug
app: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
pm : Not running
om : Not running
dm : Not running
Conversely, here's an example when the processes are running:
[WARN] : current build mode : debug
app: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
pm : root 12337 0.0 0.2 455920 9664 [more crap here]
om : root 12383 0.7 3.1 3256848 114936 [more crap here]
dm : root 12516 11.7 0.4 930316 15508 [more crap here]
[INFO] : redis [pid=5349] running @ [port:6379]
How can I parse output
based on the existing key?
Upvotes: 0
Views: 64
Reputation: 77407
You can process the output line by line looking for information you want. The output has a couple of sections but its simple enough that you can brute force it without the code being too ugly.
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
# skip [WARN]
proc.stdout.readline()
# look for interesting data in remaining
for line in proc.stdout:
# terminate after processing the [INFO] section
if line.startswith('[INFO] : redis'):
status['redis'] = True
for line in proc.stdout:
pass
break
# get the process status
name, remaining = line.split(' ', 1):
if name in status:
status[name] = not 'Not running' in remaining
proc.wait()
Upvotes: 1