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.

5
  • 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 ? Commented Jun 25, 2016 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. Commented Jun 25, 2016 at 21:49
  • I've also tried this: wrongsideofmemphis.wordpress.com/2010/03/01/… It doesn't work. (I replaced do_fancy_stuff() with os.system("mpstat 1 10 | grep Average") Commented Jun 25, 2016 at 21:50
  • Do you want to add the first two numbers that appear after "all" in the above line? Commented Jun 25, 2016 at 21:52
  • Yes. I want to add the numbers or even, just have access to them in my code. Commented Jun 25, 2016 at 21:53

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
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())
Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.