15

I am running a python code in a raspberry Pi. The code is supposed to last forever. However, after a few hours it crashes. Since it is running on a remote machine, I cannot see the message it gives during the crash.

How can I store this message on a file so I can see what was the problem? is this does autonomously in linux? or should I write some function to export the error during crash. How can I do that?

asked Feb 27, 2013 at 11:43
3
  • 3
    How are you launching your Python program exactly? Commented Feb 27, 2013 at 11:45
  • 1
    "Since it is running on a remote machine, I cannot see the message it gives during the crash." That really shouldn't be a restriction. Commented Feb 27, 2013 at 11:47
  • i ssh the raspberrypi and run the python code : sudo python file.py Commented Feb 27, 2013 at 11:49

3 Answers 3

11

You can have a main function and log in case if main function crashes

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 in case if you're using something like logstash and want to see the error and the time on your UI.

Thanks to Eric for improving the answer

answered Oct 22, 2015 at 16:00
Sign up to request clarification or add additional context in comments.

4 Comments

Use logger.exception('main crashed'), which will print the stack trace too
Feel free to edit my answer to include logger.exception
Make sure, to setup the logging at some point. Python 3.9: logger.basicConfig(filename=..., encoding='utf-8', level=logger.DEBUG). Also the library is called logging, not logger. (So for the upper code, you would need import logging as logger.)
3.8 without parameter encoding, so logger.basicConfig(filename=..., level=logger.DEBUG). See Python docs for details (in your language :)
10

You can store the output in a file, if the process is started like this:

python script.py >> /logdir/script.py.log 2>&1
answered Feb 27, 2013 at 11:48

5 Comments

is it possible to view the log on the terminal and log it on script.py.log at the same time?
@alandalusi use tail -f /logdir/script.py.log to watch the log.
It is funny that unlike running the script on terminal, the results are not put on the log file on real-time. any reason for this?
This is probably because of tail's sleep interval. You can set that like this: "tail -s 0.1 -f /logdir/script.py.log"
Its probably because of the buffering stackoverflow.com/questions/107705/python-output-buffering need to test this solution though
2

I had tried many attempts myself, but they all seemed weird...Until I figured it out! Simply write this code around your python file, and all should be well! By the way, I will be naming the crashlogs CRASH- and then the python time (e.g. CRASH-1607012036.015824.txt)

try:
 <your program here>
except Exception as e:
 crash=["Error on line {}".format(sys.exc_info()[-1].tb_lineno),"\n",e]
 print(crash)
 timeX=str(time.time())
 with open("monstergame/crashlogs/CRASH-"+timeX+".txt","w") as crashLog:
 for i in crash:
 i=str(i)
 crashLog.write(i)

Note: This is Python 3 code, not Python 2

answered Dec 3, 2020 at 16:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.