>我在 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
似乎创建新进程后的输出没有存储在变量中,我不知道为什么?
Kintul。
你的问题似乎与此非常相似:捕获python子进程的stdout stderr,当它从cron或rc.local运行时
看看这对你是否有帮助。
发生这种情况是因为 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:37319n')
('Status : FAIL', -11, 'PID:37320n')
因此,命令抛出异常被subprocess.check_output缓存而不是子进程缓存。普彭
从subprocess.check_output的官方页面摘录
如果返回代码不为零,则会引发 CalledProcessError。对象将在返回代码属性中具有返回代码,在输出属性中具有任何输出。