58

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
Drise
  • 4,310
  • 5
  • 41
  • 66
Rick
  • 16,612
  • 34
  • 110
  • 163

10 Answers10

100

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

ON SO : Popen and python

Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
25

If you're only interested in the output from the process, it's easiest to use subprocess' check_output function:


output = subprocess.check_output(["command", "arg1", "arg2"]);

Then output holds the program output to stdout. Check the link above for more info.

JoshD
  • 12,490
  • 3
  • 42
  • 53
  • also recommended, if you don't need the output, subprocess.check_call(), which checks the process status and raises an error on failure. (2.4+) But that's not an answer to *this* question, which specifically needs to capture the output. Still, one of my favorites. :) – tekHedd Feb 21 '17 at 22:37
10

The simplest way is like this:

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

This will be returned as a list

JackDev
  • 4,891
  • 1
  • 39
  • 48
8

Your code returns 0 if the execution of the commands passed is successful and non zero if it fails. The following program works on python2.7, haven checked 3 and versions above. Try this code.

>>> import commands
>>> ret = commands.getoutput("ps -p 2993 -o time --no-headers")
>>> print ret
vikkyhacks
  • 3,190
  • 9
  • 32
  • 47
2

Yes it's counter-intuitive and does not seem very pythonic, but it actually just mimics the unix API design, where these calld are C POSIX functions. Check man 3 popen && man 3 system

Somewhat more convenient snippet to replace os.system that I use:

from subprocess import (PIPE, Popen)


def invoke(command):
    '''
    Invoke command as a new system process and return its output.
    '''
    return Popen(command, stdout=PIPE, shell=True).stdout.read()


result = invoke('echo Hi, bash!')
# Result contains standard output (as you expected it in the first place).
Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
2

I can not add a comment to IonicBurger because I do not have "50 reputation" so I will add a new entry. My apologies. os.popen() is the best for multiple/complicated commands (my opinion) and also for getting the return value in addition to getting stdout like the following more complicated multiple commands:

import os
out = [ i.strip() for i in os.popen(r"ls *.py | grep -i '.*file' 2>/dev/null; echo $? ").readlines()]
print "     stdout: ", out[:-1]
print "returnValue: ", out[-1]

This will list all python files that have the word 'file' anywhere in their name. The [...] is a list comprehension to remove (strip) the newline character from each entry. The echo $? is a shell command to show the return status of the last command executed which will be the grep command and the last item of the list in this example. the 2>/dev/null says to print the stderr of the grep command to /dev/null so it does not show up in the output. The 'r' before the 'ls' command is to use the raw string so the shell will not interpret metacharacters like '*' incorrectly. This works in python 2.7. Here is the sample output:

      stdout:  ['fileFilter.py', 'fileProcess.py', 'file_access..py', 'myfile.py']
 returnValue:  0
Joe
  • 21
  • 1
1

This is an old thread, but purely using os.system, the following's a valid way of accessing the data returned by the ps call. Note: it does use a pipe to write the data to a file on disk. And OP didn't specifically ask for a solution using os.system.

>>> os.system("ps > ~/Documents/ps.txt")
0    #system call is processed.
>>> os.system("cat ~/Documents/ps.txt")
  PID TTY          TIME CMD
 9927 pts/0    00:00:00 bash
10063 pts/0    00:00:00 python
12654 pts/0    00:00:00 sh
12655 pts/0    00:00:00 ps
0

accordingly,

>>> os.system("ps -p 10063 -o time --no-headers > ~/Documents/ps.txt")
0
>>> os.system("cat ~/Documents/ps.txt")
00:00:00
0

No idea why they are all returning zeroes though.

  • the returning zeroes are the exit codes, meaning that the commands were executed succesfully – magor Apr 07 '16 at 08:24
1

For your requirement, Popen function of subprocess python module is the answer. For example,

import subprocess
..
process = subprocess.Popen("ps -p 2993 -o time --no-headers", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print stdout
Asif Hasnain
  • 179
  • 1
  • 1
0

okey I believe the fastest way it would be

import os
print(os.popen('command').readline())
x = _
print(x)
George
  • 1
  • print(os.popen('command').read()) if you want to return all the output. readline is only going to return the first line. Also worth noting that this is Python3 syntax. – aychedee May 07 '12 at 10:43
  • 1
    `_` is only set in the python interactive shell; this won't work in a script. – Martijn Pieters Oct 20 '12 at 22:12
-1
using commands module
import commands
""" 
Get high load process details 
"""
result = commands.getoutput("ps aux | sort -nrk 3,3 | head -n 1")
print result  -- python 2x
print (result) -- python 3x 
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31