C
C
cbv2020-03-12 14:46:01
Python
cbv, 2020-03-12 14:46:01

How to handle error output in a Python script on Debian?

There is a Python script that runs mosquitto via subprocess.

The program runs, but writes to Syslog:
Warning: Unable to locate configuration directory, default config not loaded.

This error is not critical and can only be removed by updating the mosquitto version, which is not possible.

Question - how can I make sure that this particular error is not written to the log, and the rest are written?

How do I run mosquitto:

try:
    subprocess.check_call(args)
except subprocess.CalledProcessError as e:
    sys.stderr.write(
    'ERROR: call to mosquitto_pub failed with error code {}\n'.format(e.returncode))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-03-12
@cbv

For example like this:

ERR = 'Warning: Unable to locate configuration directory, default config not loaded.'
try:
    output = subprocess.check_output(args)
except subprocess.CalledProcessError as e:
    sys.stderr.write(
    'ERROR: call to mosquitto_pub failed with error code {}\n'.format(e.returncode))
else:
    for line in output.decode('utf-8').split('\n'):
        if err not in line:
            print(line, file=sys.stderr)

But keep in mind, in case of an error in this option, you will not get any exhaust from your insect at all.
If the exhaust is needed in real time, then you can filter it too. It will be a little more difficult.
You will have to make a filtering proxy wrapper for stderr, which will be submitted to the subprocess.
You also need to be careful about encoding conversion. suddenly there was no utf-8. In a good way, you need to split the binary output by paragraphs and detect the lines in it ... or take the encoding from the pipe and try it.
BUT! I, here, just thought that it is easier and better to do it from the bash side. On the command line where you run your mosquito, you can add a stupid line filter with a pipe (even with a grep).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question