【问题标题】:log INFO from shell script are not displaying when we trigger shell script from python program当我们从 python 程序触发 shell 脚本时,不显示来自 shell 脚本的日志信息
【发布时间】:2019-03-25 08:01:34
【问题描述】:

我正在从我的 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 test test' 这样的输出(即 shell 脚本输出)

有人可以帮我提供一些信息,我怎样才能获得所有的 shell 脚本输出。

【问题讨论】:

    标签: python shell process subprocess


    【解决方案1】:

    您的脚本似乎正在将 INFODEBUG 日志发送到 stderr。您可以尝试仅删除stderr=subprocess.PIPE,以查看输出是否出现在终端中。

    如果您确实需要 stderrsubprocess.PIPE,假设您想从 Python 捕获 stderr 输出,则需要调用 process.communicate() 查看更多信息 Popen.communicate

    【讨论】:

    • 非常感谢@beezz 的回复。你是对的,我删除了 stderr 并且可以在终端中看到输出。我修改了我的脚本以读取 'stderr' 而不是 'stdout'。现在它工作正常。
    猜你喜欢
    • 2011-05-21
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多