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 screen, I don’t get access to the error when the scrips crashes. Is there any way to extract that info through logging or a simple pipe?
2 Answers
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 &
2 Comments
Buzzet
that seems good. Can I run multiple scrips in paralell and different output files with this?
B.C
Yes, this should be possible. However, you will need to be careful not to mess up your
nohup.out file if you stick to the default as it will just append the output to the file so you could end up with a mess.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 here
except 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