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,390 points
48 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
• 57,510 points

Related Questions In Python

0 votes
1 answer

What to do when I get and error saying python not recognized as internal or external command?

You need to set up the path ...READ MORE

answered May 28, 2019 in Python by Fata
• 1,040 points
73 views
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, 2019 in Python by Neel
• 3,010 points
70 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, 2019 in Python by Arvind
• 2,980 points
77 views
0 votes
1 answer

Can we download python from command line?

Open a command prompt window and navigate to the ...READ MORE

answered Aug 6, 2019 in Python by Mohammad
• 3,210 points
57 views
0 votes
1 answer
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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 547 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
• 57,510 points
151 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
• 57,510 points
105 views