【发布时间】:2020-02-21 15:16:26
【问题描述】:
我正在使用 Airflow,并且我有一些 python 脚本,其中一个启动 sh 脚本(1 个或多个)。对于airflow,即使这个bash脚本返回错误,它也认为任务是成功的。
我想在我的 python 脚本中返回 bash 错误,因为气流会注意到。
import glob
import os
from os import path
import subprocess
odsPath = '/apps/data/02_OS/'
for odsFolder in os.listdir(odsPath):
if(odsFolder in fileNames):
newPath = odsPath+odsFolder
print('Script path: ', newPath)
for foldFiles in os.listdir(newPath):
if(foldFiles.endswith('sh')):
print('Sh script can be launch: ', foldFiles)
if(foldFiles.startswith('run') and foldFiles.endswith('.sh')):
os.system('sh ' + newPath+'/'+foldFiles)
print('Script: '+foldFiles+' executed')
在这种情况下,存在 bash 错误,例如“参数数量不正确”
感谢您的帮助:)
【问题讨论】:
-
你试过什么?您想捕获“STDERR”输出(或者如果脚本顽皮并使用错误的输出流,也可能是“STDOUT”)en.wikipedia.org/wiki/Standard_streamsdocs.python.org/2/library/subprocess.html