When using subprocess.Popen, the output of mongoexport is in stderr

277 Views Asked by At

I'm trying to export my MongoDB collection into a JSON file, and according to subprocess.Popen.communicate() I can use something like this:

from time import time
from subprocess import Popen, PIPE, TimeoutExpired

filename = f"products - {int(time())}.json"
try:
    process = Popen(
        [
            "mongoexport",
            "--jsonArray",
            "--pretty",
            f"--db=dbname",
            "--collection=products",
            f"--out={filename}",
        ],
        stdout=PIPE,
        stderr=PIPE
    )
    stdout, stderr = process.communicate(timeout=10)
    print(str(stdout))
    print()
    print(str(stderr))
except TimeoutExpired:
    error_message = (
        "commands.py - export() - "
        "timeout - killing process"
    )
    logging.error(error_message)
    process.kill()
except Exception as e:
    error_message = (
        "commands.py - export() - "
        f"couldn't export - {e}"
    )
    logging.error(error_message)

The program runs successfully and the file is created, but those stdout and stderr variables are not what I exptected. This is printed to the console:

b''

b'2021-05-23T12:02:12.119+0430\tconnected to: mongodb://localhost/\n2021-05-23T12:02:12.242+0430\texported 1761 records\n'

So stdout is empty and the successful message is in stderr. But the order of tuples is the same as the example given in the link I provided:

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

What's going on?

I'm asking this because I'm trying to further check the output of stderr to make sure everything went according to plan.

1

There are 1 best solutions below

2
On

stderr is often used for logging, with stdout being used for program output.

Thus, for a program that dumps a database, the dump itself might be printed to standard output and the informational messages printed to standard error.