0

I'm trying to debug a python script using logger. It opens another python script and checks whether it's running or not. If not then it restarts that script.

My script:

while True:
    print("Restart")
    logger.info("Restart")

    try:
        p = subprocess.Popen(["python", OPEN_FILE]).wait()
    except:
        logger.exception("Error opening script")
    print("Exit")
    logger.error("Exit")
    time.sleep(10)
    if p != 0:
        continue
    else:
        break

If file is not found then it prints error on terminal:

pi@raspberrypi:~/Desktop/MODBUS_TCP $ sudo python py_restart_script.py
Restart
python: can't open
file'/home/pi/Desktop/MODBUS_TCP/API_Modbus_TCP_Server3.py': [Errno 2] No
such file or directory
Exit

But that error is not in the log file:

2018-11-15 22:30:16,269 - INFO - Restart
2018-11-15 22:30:16,325 - ERROR - Exit

How to log same error showing in terminal to the log file?

3
  • Change your code to except Exception as exc: and then use something like logger.error(str(exc)) to record it in the log file. Commented Nov 15, 2018 at 17:57
  • I have tried but no success. Commented Nov 16, 2018 at 17:02
  • That's because no exception is occurring in your restart.py script. It's happening in the spawned process which is running a separate copy of the python interpreter and the failure to be able to do that is just the return of a non-zero status code. @isalgueiro's answer should allow you to detect and capture the error output from that happening and put it in the log. Commented Nov 16, 2018 at 18:39

1 Answer 1

1

What's printed in the terminal is process stderr output. You can get it in your python script and print it in the log file

>>> import subprocess
>>> p = subprocess.Popen(["python", "foobar"], stderr=subprocess.PIPE)
>>> ret = p.wait()
>>> ret # this holds the `subprocess` return code. ret != 0 indicates an error
2
>>> (_, stderr) = p.communicate()
>>> stderr
"python: can't open file 'foobar': [Errno 2] No such file or directory\n"
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.