Search code examples
pythonsystemtermination

launching python script process from another script on Ubuntu: how to track status?


I launch one Python script out of another one on an Amazon EC2 Ubuntu instance using a command:

os.system(call) 

where call has the form

"./script2.py arg1 arg2 arg3"

I've noticed that from time to time script2.py somehow terminates prematurely. I have some logging statements in it but they don't show what's going on. So my questions are:

  1. I read that system() returns some sort of exit status. What's the best test to distinguish between a normal and abnormal termination? That is, I only want to produce a log message inside the calling script if there is some sort of abnormal termination.
  2. Is there any sort of system log where I could try to find traces of terminated processes?

Solution

  • Assuming that your child script does not use standard output for something and returns non-zero on a crash (this should be default for python executable though):

    import subprocess as sp
    
    try:
        sp.check_output(['python', 'child.py'], stderr=sp.STDOUT)
    except sp.CalledProcessError as err:
        print 'Child terminated abnormally with error %d, log:' % err.returncode
        print err.output