当我们触发来自 python 程序的 shell 脚本时,来自 shell 脚本的日志信息不显示
log INFO from shell script are not displaying when we trigger shell script from python program
我正在从我的 pyhton 程序调用 shell 脚本,shell 脚本将 INFO 和 DEBUG 日志作为输出的一部分。
当我 运行 来自 python 程序的 shell 脚本时,我只能看到标准输出,但看不到 shell 脚本输出的 INFO 和 DEBUG 日志。
我的代码:
process = subprocess.Popen(['bash','/myshell_script.sh',env, params],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while (True):
retcode = process.poll() # returns None while subprocess is running
line = process.stdout.readline()
**print(line)**
if (retcode is not None):
break
这里,当我打印 shell 脚本输出时,我只能看到部分输出,看不到像“18/10/20 11:24:55 INFO 测试测试”这样的输出(这是 shell 脚本输出)
谁能帮我提供一些信息,我怎样才能获得所有 shell 脚本输出。
您的脚本似乎正在将 INFO
和 DEBUG
日志发送到 stderr
。您可以尝试仅删除 stderr=subprocess.PIPE
,以查看输出是否出现在终端中。
如果您确实需要 stderr
的 subprocess.PIPE
,假设您想从 Python 捕获 stderr
输出,则需要调用 process.communicate()
查看更多信息 Popen.communicate
我正在从我的 pyhton 程序调用 shell 脚本,shell 脚本将 INFO 和 DEBUG 日志作为输出的一部分。 当我 运行 来自 python 程序的 shell 脚本时,我只能看到标准输出,但看不到 shell 脚本输出的 INFO 和 DEBUG 日志。
我的代码:
process = subprocess.Popen(['bash','/myshell_script.sh',env, params],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while (True):
retcode = process.poll() # returns None while subprocess is running
line = process.stdout.readline()
**print(line)**
if (retcode is not None):
break
这里,当我打印 shell 脚本输出时,我只能看到部分输出,看不到像“18/10/20 11:24:55 INFO 测试测试”这样的输出(这是 shell 脚本输出)
谁能帮我提供一些信息,我怎样才能获得所有 shell 脚本输出。
您的脚本似乎正在将 INFO
和 DEBUG
日志发送到 stderr
。您可以尝试仅删除 stderr=subprocess.PIPE
,以查看输出是否出现在终端中。
如果您确实需要 stderr
的 subprocess.PIPE
,假设您想从 Python 捕获 stderr
输出,则需要调用 process.communicate()
查看更多信息 Popen.communicate