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

假如想象 提交于 2019-12-11 20:21:45

问题


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?

回答1:


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



回答2:


1> os.system returns the system code if it returns 0 process ran and terminated successfully but if it returns any number then generally it means an error but it depeds on how progam responds to the errors what i mean is if progam returns 1 on success os.system will return 1

for example

d = os.system('ls')
print d 
d = os.system(your process)
print d

2> python provides syslog module you can try it like

>>>import syslog
>>>help(syslog)

syslog is a buil-in unix api provided by the os itself

http://docs.python.org/2/library/syslog.html



来源:https://stackoverflow.com/questions/13822480/launching-python-script-process-from-another-script-on-ubuntu-how-to-track-stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!