launching python script process from another scrip

2019-09-07 16:49发布

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?

2条回答
聊天终结者
2楼-- · 2019-09-07 17:20

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
查看更多
混吃等死
3楼-- · 2019-09-07 17:26

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

查看更多
登录 后发表回答