2

I'm using python to script a functional script and I can't handler the result of this command line:

os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name)

It shows me pids but I can't use it as List.

If I test:

pids = os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name)
print type(pids)

#Results
29719
30205
31037
31612
<type 'int'>

Why is pids an int? How can I handle this result as List?

Stranger part:

print type(os.system("ps aux -u %s | grep %s | grep -v 'grep' | awk '{print $2}'" % (username, process_name))

There is nothing. Not any type written on my console..

brcebn
  • 1,571
  • 1
  • 23
  • 46
  • possible duplicate of [Python, os.system for command-line call (linux) not returning what it should?](http://stackoverflow.com/questions/3791465/python-os-system-for-command-line-call-linux-not-returning-what-it-should) – Holt Jul 07 '15 at 07:48
  • @Holt This is close to a duplicate, but the linked question does not have `shell=True`, which will break considering the OP is using pipes here. Did you find any with `shell=True`? – Thomas Orozco Jul 07 '15 at 07:49
  • @ThomasOrozco This one (http://stackoverflow.com/questions/6276614/store-os-system-result-in-variable?rq=1) has a `shell=True` using `Popen`, even if it is not needed by the other OP. My though is that there are plenty of posts on SO (and elsewhere) asking for `How to get the result of a shell command?`, and adding new ones just makes everything confusing for someone looking for an answer (IMO). – Holt Jul 07 '15 at 07:52

2 Answers2

6

os.system does not capture the output of the command it runs. To do so you need to use subprocess.

from subprocess import check_output

out = check_output("your command goes here", shell=true)

The above will work in Python 2.7. For older Pythons, use:

import subprocess
p = subprocess.Popen("your command goes here", stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Can you share information where it is written in official documentation? – Khamidulla Jul 07 '15 at 07:49
  • That's good. Thank you! I've added this line to have a List `out.split('\n')`. By the way, there is a typing error on the first command (shell=True) with a capital letter. – brcebn Jul 07 '15 at 08:05
0

os module documentation

os.system(command)

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

If you want access to the output of the command, use the subprocess module instead, e.g. check_output:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

Run command with arguments and return its output as a byte string.

valhallasw
  • 341
  • 1
  • 5