Ubuntu – python script crash prevent/reason check

pythonUbuntu

I run a python script several days ago using 'python a.py &' on my Ubuntu 12.04. I assume it's running all the time, but just now I found it crashed 2 days ago.
Is there any log information on ubuntu that I can find how it crashed? Any debug message recorded by the system?

Also, is there a way to set up a alert checking, like send me an email if this python script is not running anymore? I need it to be running all the time.
Thanks

Best Answer

nohup python a.py 2>&1 > myscriptoutput &

In this case stderror/out will go to that file "myscriptoutput" and will not use tty. Once you close your ssh session generated HUP signal will be ignored by python process. And as of monitoring your script: put a cron job to run every 2 minutes and do something like this:

    #!/bin/bash
    if ps -aux | grep -v grep | grep a.py; then
        exit 0
    else
        #Generate email
        echo "Script a.py is not running" | mailx -s "My script is dead a.py" admin@mycomp.org 
        exit 0
fi

There are actually much better ways to do it of course but this is will get you started.

Hope this helps.

Related Topic