0

I'm running an embedded application on a RPI. I am planning to have a script restart it if it crashes. But I'd like to see the crash info. So I see how to redirect stderr to a file to append the latest crash details but I would like to add a time tag for each crash situation. 1) Is there an easy way to do this? I wrote a simple python program to write out a time tag followed by reading from stdin and echoing the data out. 2) How can I redirect just the stderr output to my program? I tried the following command line but it doesn't work. Not sure proper way to do this.

sudo python3 TestRedirect.py 2> | sudo python3 TimeTagErrors.py

Maybe the answer to question 1 will make question 2 not necessary.

1

Seems to me that you should simply:

  1. add exception handling in your Python program - if you aren't already you should
  2. log exceptions to a file, for this use the logging module. See here: Basic Logging Tutorial and here: Logging Cookbook for an intro

That should be enough.

To have the script restart upon failure: one way is to write your own systemd service.

If you really want to capture console output I would suggest running your script in a GNU screen session. Then you easily capture all console output to a log file.

2
  • 1
    A systemd service will log output to syslog with a timestamp.
    – Dougie
    Jun 29 '20 at 7:51
  • It's a bit of a misnomer to refer to capturing "console output" here. It's output that would go to a console if the script were run from the command line (ie, in a console context). The script/program doesn't really do that itself; it simply writes to stdout/stderr (which are intrinsic properties of a running process), and this is captured by the console. Point being, using screen would indeed allow you to run the program in a console context (screen playing the console) but this is neither necessary nor generally desirable (unless the output is formatted specially for a console).
    – goldilocks
    Jun 29 '20 at 14:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.