从Ubuntu上的另一个脚本启动python脚本进程:如何跟踪状态? [英] launching python script process from another script on Ubuntu: how to track status?

查看:203
本文介绍了从Ubuntu上的另一个脚本启动python脚本进程:如何跟踪状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令在Amazon EC2 Ubuntu实例上启动另一个Python脚本:

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

os.system(call) 

其中call具有形式

"./script2.py arg1 arg2 arg3"

我已经注意到,不时地script2.py会过早终止.我有一些日志记录语句,但是它们没有显示正在发生的事情.所以我的问题是:

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. 我读到system()返回某种退出状态.区分正常终止和异常终止的最佳测试是什么?也就是说,如果发生某种异常终止,我只想在调用脚本内生成一条日志消息.
  2. 是否存在任何类型的系统日志,我可以在其中找到终止的进程的痕迹?
  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?

推荐答案

假设您的子脚本未对某些内容使用标准输出,并且在崩溃时返回非零值(不过,对于python可执行文件,这应该是默认值):

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

这篇关于从Ubuntu上的另一个脚本启动python脚本进程:如何跟踪状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆