4

I am trying to write a code in python that will take some information from top and put it into a file. I want to just write the name of the application and generate the file. The problem i am having is that i can't get the output of the pidof command so i can use it in python. My code looks like this :

import os

a = input('Name of the application')
val=os.system('pidof ' + str(a)) 
os.system('top -d 30 | grep' + str(val) + '> test.txt')
os.system('awk '{print $10, $11}' test.txt > test2.txt')

The problem is that val always has 0 but the command is returning the pid i want. Any input would be great.

1
  • You have the name of the application and you want... what? Write something concerning this application into a file? Commented Apr 21, 2011 at 13:56

2 Answers 2

8

First up, the use of input() is discouraged as it expects the user to type in valid Python expressions. Use raw_input() instead:

app = raw_input('Name of the application: ')

Next up, the return value from system('pidof') isn't the PID, it's the exit code from the pidof command, i.e. zero on success, non-zero on failure. You want to capture the output of pidof.

import subprocess

# Python 2.7 only
pid = int(subprocess.check_output(['pidof', app]))

# Python 2.4+
pid = int(subprocess.Popen(['pidof', app], stdout=subprocess.PIPE).communicate()[0])

# Older (deprecated)
pid = int(os.popen('pidof ' + app).read())

The next line is missing a space after the grep and would have resulted in a command like grep1234. Using the string formatting operator % will make this a little easier to spot:

os.system('top -d 30 | grep %d > test.txt' % (pid))

The third line is badly quoted and should have caused a syntax error. Watch out for the single quotes inside of single quotes.

os.system("awk '{print $10, $11}' test.txt > test2.txt")
Sign up to request clarification or add additional context in comments.

3 Comments

This is the answer you want. You can abstract it into a function if you desire. However note that you may possibly need to call .strip() to remove newlines on the output: .communicate()[0].strip() -- you don't need to in this case, since int('12\n')==12
Thank you for the solution it works now. You help me a lot and again thank you for your fast time responce. I am using python3 and the raw_input it's not supported or i don't know if has a new form.
the equivalent in python3 of raw_input is input there is no equivalent of input of 2.X
2

Instead of os.system, I recommend you to use the subprocess module: http://docs.python.org/library/subprocess.html#module-subprocess

With that module, you can communicate (input and output) with a shell. The documentation explains the details of how to use it.

Hope this helps!

Comments

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.