Catching error codes of python processes outside of them in linux?

0

I am using a python code, not mine, that runs for days or even weeks. It might have some bugs left, so it might crash once in a while. But I dont want to check it every couple of hours. I know that the python process(es) do return codes !=0 if something happens.

But is it possible to catch that from outside of the processes? If yes, I would like to do that, so I can send an Email to myself if something happens.

Thx.

Pythoneer

Posted 2018-09-20T14:55:00.423

Reputation: 73

Answers

0

All you need is to check the return code, using something like:-

if PythonScript then
    # Actions on success
    ...
else
    # Actions on failure
    err=$?
    ...
fi

Note that the return code $? is overwritten by every command that is executed, so if its value is needed for logging or other purposes you need to save it before a subsequent command changes it.

If the PythonScript is executable and has the first line #!/usr/bin/python (or the path to the python version to be used), then it can be called directly; otherwise, it should be preceded by python or the appropriate version.

AFH

Posted 2018-09-20T14:55:00.423

Reputation: 15 470

Could you please elaborate? If I am running a.py, which takes ages, I run then b.py which is able to check if a.py returns error messages? Because I don't want to mess around in a.py – Pythoneer – 2018-09-20T16:35:30.027

Your question and comment are unclear. How do you pass any error to b.py? In a run parameter? in an environment variable? in a file? I can't elaborate my answer until you elaborate your question. If you will always run b.py, then the if in my answer is unnecessary: it could be as simple as a.py; b.py $?, where b.py is passed the completion code from a.py in its first parameter. – AFH – 2018-09-20T16:55:41.670

No, thats the point. I start screen, start python a.py, and then let it run its course. Thats all I do. if a.py crashes, it will be seen in the screen, because it will print stuff like returned 1 ... and I would somehow like to catch this... – Pythoneer – 2018-09-20T18:00:32.103

I assumed that a.py calls sys.exit() in order to return the completion status. If it doesn't, you'll have a lot of difficulty in reading strings from a window: although you don't want to modify a.py, this is probably the easiest answer. – AFH – 2018-09-20T18:33:55.120