2

I'm using the subprocess module from python and whenever I call any of the methods that subprocess has to run, in this case some bash code on the shell, the output (lets say from a command e.g iwconfig) is redirected to stderr.

Shouldn't the output from that command be redirected to stdout instead of to stderr? As I said, this happens on any method like .Popen() and .check_output(). This question might seem duplicated, but I don't find any other answers on the forum that explains the concept of why this happens.

The stderr parameter is set to subprocess.STDOUT so I can grab the output from the command. Otherwise there is no other way. In any case, stderr is where the output errors go, right? This does not make sense for me...

s = subprocess.check_output("iwconfig", shell=True, universal_newlines=True, stderr=subprocess.STDOUT)

Thanks in advance.

VladiC4T
  • 216
  • 2
  • 10
  • 1
    Some output from `iwconfig` will go to `stderr`. For example, if you have no wireless device, then all of `iwconfig` output is an error message, and sent to `stderr`. – John Anderson Apr 14 '19 at 21:12
  • 1
    I'm not sure I get what you're asking. Who is doing this "redirection" you are talking about? Are you just talking about what what the subprocess module does with stderr and stdout? That's not redirection really. This is why I'm confused. – CryptoFool Apr 14 '19 at 22:05
  • 1
    In general, linux (and Mac) command line binaries send regular results to stdout, and error messages to stderr. For example, "ls /foo/bar" will list the path /foo/bar to stdout if it refers to an existing object, and will send an error message to stderr if it does not exist. If you run this command via one of the subprocess module's functions using "shell=True", you will see these same results mapped to its stdout and stderr parameters respectively. - I know of no strange redirection behavior in any of this. – CryptoFool Apr 14 '19 at 22:18
  • If this is just a question of strange behavior of `iwconfig` specifically, then it sounds like @JohnAnderson has you covered. – CryptoFool Apr 14 '19 at 22:23
  • I think it is more/less standard linux/unix behavior to redirect outputs to stderr (I tested with my own script run by subprocess). However, there may be something I miss (like shell=True/False parameter settings). – Edward Aung Apr 15 '19 at 00:24
  • 1
    I still don't understand what this "standard linux/unix behavior to redirect outputs to stderr" is about. I asked above, and was hoping someone could explain it to me. If anything, my experience has been the opposite...that stderr is often redirected to stdout. What "redirection" are we talking about? - What does this have to do with Python and the `subprocess` module? – CryptoFool Apr 15 '19 at 00:39

1 Answers1

0

As @JohnAnderson said, this happens because, if there is no wireless device on my system configured, the output from iwconfig will be viewed as an error message even though the output seemed to be normal. I also found another new method from subprocess called run() and the same happens, so definitely it is a Unix/Linux behavior.

s = subprocess.run('iwconfig', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

s.stderr #Produces output
VladiC4T
  • 216
  • 2
  • 10