Skip to content Skip to sidebar Skip to footer

Log When Python Script Failed

I am running a rather complex python script through a screen session on my server. Sometimes, after some hours, it crashes and I can’t figure out why. Since the script runs in sc

Solution 1:

You can use nohup and & to run your python command in the background. The default output will log any printed outputs (including the error that terminates your script) in a nohup.out file. You can specify the file output in Bash using >

For example

nohup python my_python_script.py > my_output_log &

Solution 2:

I had a very similar problem and I did something like this

import logging
logging.basicConfig(filename = os.path.join(script_path, 'error.log'), level = logging.ERROR)
try:
    # some dangerous code hereexcept Exception as e:
    logging.exception(str(e))
    # Now you can kill your script, etc

This is going to generate a file 'error.log' with the info of the crash

Post a Comment for "Log When Python Script Failed"