Linux Command-Line Call Not Returning What It Should from Os.System

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

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

Handle result of os.system

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()

Python, os.system and subprocess.call both don't work for variable string command

Just to make sure I don't leave everyone hanging, I figured out the solution. It's very strange. All I did was call the parameters in a different order, and now it works every time. They work in any order if I input the command manually, but if I issue the command from python it seems it will only work if I write the output parameter first. Not sure if this is a problem with python or with the application I'm writing the command to.

Python os.system() returning bad value

So the short answer is that the value I'm getting is the exit code from the program, unless Windows feels it has something important to say.

In this case a DLL I was calling was corrupting the heap and cause windows to abort the execution. To get the actual 32 bit Windows return code and not a sign extended version I needed to AND it with 0xffffffff. I don't know if I can answer my own question, but gelonida set me on the right trail the answer.

What is the return value of os.system() in Python?

The return value of os.system is OS-dependant.

On Unix, the return value is a 16-bit number that contains two different pieces of information. From the documentation:

a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero)

So if the signal number (low byte) is 0, it would, in theory, be safe to shift the result by 8 bits (result >> 8) to get the error code. The function os.WEXITSTATUS does exactly this. If the error code is 0, that usually means that the process exited without errors.

On Windows, the documentation specifies that the return value of os.system is shell-dependant. If the shell is cmd.exe (the default one), the value is the return code of the process. Again, 0 would mean that there weren't errors.

For others error codes:

  • on Linux
  • on Windows

Hiding console output produced by os.system

To answer the question based on its title in the most generic form:

To suppress all output from os.system(), append >/dev/null 2>&1 to the shell command, which silences both stdout and stderr; e.g.:

import os
os.system('echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null 2>&1')

Note that os.system() by design passes output from the calling process' stdout and stderr streams through to the console (terminal) - your Python code never sees them.

Also, os.system() does not raise an exception if the shell command fails and instead returns an exit code; note that it takes additional work to extract the shell command's true exit code: you need to extract the high byte from the 16-bit value returned, by applying >> 8 (although you can rely on a return value other than 0 implying an error condition).


Given the above limitations of os.system(), it is generally worthwhile to use the functions in the subprocess module instead:

For instance, subprocess.check_output() could be used as follows:

import subprocess
subprocess.check_output('echo 3 | sudo tee /proc/sys/vm/drop_caches', shell=True)

The above will:

  • capture stdout output and return it (with the return value being ignored in the example above)
  • pass stderr output through; passing stderr=subprocess.STDOUT as an additional argument would also capture stderr.
  • raise an error, if the shell command fails.

Note: Python 3.5 introduced subprocess.run(), a more flexible successor to both os.system() and subprocess.check_output() - see https://docs.python.org/3.5/library/subprocess.html#using-the-subprocess-module


Note:

  • The reason that the OP is employing tee in the first place - despite not being interested in stdout output - is that a naïve attempt to use > ... instead would be interpreted before sudo is invoked, and thus fail, because the required privileges to write to /proc/sys/... haven't been granted yet.
  • Whether you're using os.system() or a subprocess function, stdin is not affected by default, so if you're invoking your script from a terminal, you'll get an interactive password prompt when the sudo command is encountered (unless the credentials have been cached).

Give response yes/no in python when a command is executed os.system() in python linux

You can use subprocess.Popen and write to stdin, you need the -S flag for sudo then just the rest of the commands.

from subprocess import Popen, PIPE
import getpass

pwd = getpass.getpass()
proc = Popen(['sudo', '-S', rest of commands ],stdout=PIPE, stdin=PIPE, stderr=PIPE,universal_newlines=True)
proc.stdin.write("{}\n".format(pwd))
out,err = proc.communicate(input="{}\n".format("yes"))


Related Topics



Leave a reply



Submit