How to register a Python crash?

I am running python code in a raspberry Pi. The code should last forever. However, after a few hours it falls. Since it works on a remote machine, I do not see the message that it gives during the crash.

How to save this message in a file so that I can understand what the problem is? is it standalone in linux? or should I write some function to export the error during the failure. How can i do this?

+4
source share
2 answers

You can save the output in a file if the process starts as follows:

python script.py >> /logdir/script.py.log 2>&1 
+6
source

You may have a main function and a log in case the main function works

 def main(): ... raise ValueError("crashed because I'm a bad exception") ... if __name__ == "__main__": try: main() except Exception as e: logger.exception("main crashed. Error: %s", e) 

This is better if you use something like logstash and want to see the error and time of your user interface.

Thanks Eric for improving the answer

+3
source

All Articles