我在unix环境中调用一些包装在python脚本中的java二进制文件。
当我从bash调用脚本时,输出是干净的,并且也存储在所需的变量中,但是,当我从Cron运行相同的脚本时,存储在变量中的输出是不完整的
我的代码:
command = '/opt/HP/BSM/PMDB/bin/abcAdminUtil -abort -streamId ETL_' \
'SystemManagement_PA@Fact_SCOPE_OVPAGlobal'
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(output, err) = proc.communicate() # Storing Output in output variable从shell运行时输出变量的值:
Abort cmd output:PID:8717
Executing abort function
hibernateConfigurationFile = /OBRHA/HPE-OBR/PMDB/lib/hibernate-core-4.3.8.Final.jar
Starting to Abort Stream ETL_SystemManagement_PA@Fact_SCOPE_OVPAGlobal
Aborting StreamETL_SystemManagement_PA@Fact_SCOPE_OVPAGlobal从cron运行时输出变量的值:
PID:830创建新进程后的输出似乎没有存储在变量中,我不知道为什么?
发布于 2019-04-01 22:54:02
金图尔。
你的问题看起来和这个很相似:Capture stdout stderr of python subprocess, when it runs from cron or rc.local
看看这对你有没有帮助。
发布于 2019-04-03 19:15:25
发生这种情况的原因是,subprocess.Popen没有缓存Java实用程序执行特洛伊木马的异常
但是,异常被subprocess.check_output捕获
更新代码:
try:
output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
except subprocess.CalledProcessError as exc:
print("Status : FAIL", exc.returncode, exc.output)
else:
print("Output of Resume cmd: \n{}\n".format(output))
file.write("Output of Resume cmd: \n{}\n".format(output) + "\n") 代码的输出:
('Status : FAIL', -11, 'PID:37319\n')
('Status : FAIL', -11, 'PID:37320\n')因此,命令抛出异常由subprocess.check_output缓存,而不是由subprocess.Popen缓存
subprocess.check_output的提取表单官方页面
如果返回代码为非零,则会引发CalledProcessError。CalledProcessError对象将在返回代码属性中包含返回代码,并在输出属性中包含所有输出。
https://stackoverflow.com/questions/55454569
复制相似问题