Linux command-line call not returning what it should from os.system?

0 votes

I need to make some command line calls to linux and get the return from this, however doing it as below is just returning 0 when it should return a time value, like 00:08:19, I am testing the exact same call in regular command line and it returns the time value 00:08:19 so I am confused as to what I am doing wrong as I thought this was how to do it in python.

import os retvalue = os.system("ps -p 2993 -o time --no-headers") print retvalue

Aug 29, 2018 in Python by bug_seeker
• 15,310 points
29 views

1 answer to this question.

0 votes

What gets returned is the return value of executing this command. What you see in while executing it directly is the output of the command in stdout. That 0 is returned means, there was no error in execution.

Use popen etc for capturing the output .
Some thing along this line:

import subprocess as sub
p = sub.Popen(['your command', 'arg1', 'arg2',...],stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate() print output

or

import os
p = os.popen('command',"r")
while 1:
    line = p.readline()
    if not line:
    break
    print line

answered Aug 29, 2018 by Priyaj
• 56,160 points

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26 in Python by Neel
• 1,350 points
26 views
0 votes
1 answer

Is it possible to call one python script from another Python Script?

Yes it is possible. Suppose your script abc.py ...READ MORE

answered Jul 2 in Python by Arvind
• 1,290 points
12 views
+4 votes
7 answers
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 122 views
0 votes
1 answer

'python' is not recognized as an internal or external command

I have met same issue when I ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,160 points
86 views
0 votes
1 answer

Restart python script automatically even when it crashes in Linux

Maybe this would be more robust? 1) save ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 56,160 points
60 views