1

When I do, in python code,

os.system("mpstat 1 10 | grep Average")

I get, at stdout:

"Average: all 0.00 0.00 0.00 0.00" and a bunch of other things

What can I add to

mpstat 1 10 | grep Average | SOMETHING

to get a variable that contains the line that starts with Average?

I need the sum of the first number and the second number.

I tried the accepted answer here:

Can I redirect the stdout in python into some sort of string buffer?

But it doesn't work.

Community
  • 1
  • 1
Jobs
  • 3,317
  • 6
  • 26
  • 52
  • You are already getting the line that starts with Average. What are you looking for ? What do you mean by first number and second number ? – UltraInstinct Jun 25 '16 at 21:45
  • The first number is 0.00, and the second number is 0.00 in this case. I need to save the line in a string / variable so I can parse it or use it later in my python code. – Jobs Jun 25 '16 at 21:49
  • I've also tried this: https://wrongsideofmemphis.wordpress.com/2010/03/01/store-standard-output-on-a-variable-in-python/ It doesn't work. (I replaced do_fancy_stuff() with os.system("mpstat 1 10 | grep Average") – Jobs Jun 25 '16 at 21:50
  • Do you want to add the first two numbers that appear after "all" in the above line? – UltraInstinct Jun 25 '16 at 21:52
  • Yes. I want to add the numbers or even, just have access to them in my code. – Jobs Jun 25 '16 at 21:53

1 Answers1

2

Do away with os.system call. Use subprocess.Popen instead. Here's how you'd do it:

>>> from subprocess import Popen
>>> from subprocess import PIPE
>>> mpstat = Popen(["mpstat", "1", "10"], stdout=PIPE)
>>> grep = Popen(["grep", "Average"], stdin=mpstat.stdout, stdout=PIPE)
>>> mpstat.stdout.close()
>>> res, err = grep.communicate()
>>> res
'Average:     all    0.79    0.00    0.46    0.03    0.00    0.01    0.00    0.00    0.00   98.71\n'
>>> res.strip().split()
['Average:', 'all', '0.79', '0.00', '0.46', '0.03', '0.00', '0.01', '0.00', '0.00', '0.00', '98.71']
>>> res.strip().split()[2:4]
['0.79', '0.00']
>>> values = map(float, res.strip().split()[2:4])
>>> values
[0.79, 0.0]
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
  • Popen is not blocking. I want to kill another process with pkill -f name immediately after I get the "values" - [0.79, 0.0]. So, is it safe to say pkill -f name in the line below >>values in your example? If so, how does it work, since Popen is not blocking. There's gotta be something that IS blocking for this to work. – Jobs Jun 25 '16 at 22:40
  • After `communicate(..)` your child processes (mpstat and grep) have actually exited. If you wanted to kill those processes -- dont -- not needed. If you want to kill another process unrelated process, you can do it anytime. – UltraInstinct Jun 25 '16 at 22:44
  • So communicate() is what makes this blocking, in the sense that, if the mpstat duration is 200 seconds, it will take 200 seconds for everything up to the line after communicate() to be executed? Yes, I want to kill another unrelated process that mpstat is measuring immediately after I get the "values" results. So, from what I understood, It would be okay to kill that process (the unrelated process that mpstat is measuring) after the code above (or after communicate()) – Jobs Jun 25 '16 at 22:47
  • Thank you so much! – Jobs Jun 25 '16 at 23:05