从 bash 脚本调用时获取 Python 命令行输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24673096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:50:32  来源:igfitidea点击:

Get Python command line output when called from a bash script

pythonbashshellubuntu

提问by JJC

This may be a super simple question. I am calling a Python script on an Lubuntu OS (Cubietruck) via a shell script. This is to automate the process on start up (I want to maintain this process). Is there a simple way to view the output from Python? At the moment the Python script runs in the background, with no terminal. I have some errors that need checking. The script is:

这可能是一个超级简单的问题。我通过 shell 脚本在 Lubuntu OS (Cubietruck) 上调用 Python 脚本。这是为了在启动时自动化该过程(我想维护这个过程)。有没有一种简单的方法可以查看 Python 的输出?目前 Python 脚本在后台运行,没有终端。我有一些错误需要检查。脚本是:

#!/bin/sh
python recordSound.py

Thanks in advance.

提前致谢。

回答by Sylvain Leroux

The proper solution would probably use loggingas proposed in a comment.

正确的解决方案可能会logging按照评论中的建议使用。

In the meantime, you have to redirect boththe standard outputand the standard errorstream in order to capture the normal output as wall as any error reported by your script:

在此期间,你必须重定向两个标准输出和标准错误,以捕捉正常输出为壁所报告的脚本任何错误流:

#!/bin/sh
python recordSound.py >> logfile.log 2&>1

See one of the many web pages on that topicto explore the various redirection available from a shell script.

请参阅有关该主题的众多网页之一,以探索可从 shell 脚本获得的各种重定向。



In addition, if you need both login andlive view on the console, use the teestandard command:

此外,如果您需要在控制台上同时登录实时查看,请使用tee标准命令:

#!/bin/sh
python recordSound.py 2&>1 | tee logfile.log

回答by Cediddi

Call your script directly. Just like you call bash script with a shebang (#!) you can call your python script with a #!/bin/env python. Then all you need to do is giving executable permission via chmod +x yourscript.pyand calling your script directly like an application. This way, you can see the error codes. Another way to do is using logger module, which is great to see tracebacks and debug info.

直接调用您的脚本。就像你用 shebang ( #!)调用 bash 脚本一样,你可以用#!/bin/env python. 然后您需要做的就是通过授予可执行权限chmod +x yourscript.py并像应用程序一样直接调用您的脚本。这样,您就可以看到错误代码。另一种方法是使用 logger 模块,它非常适合查看回溯和调试信息。

回答by PhilDenfer

I'm no bash expert, but I think this should work, as python prints on the standart output unless told otherwise :

我不是 bash 专家,但我认为这应该可行,因为除非另有说明,否则 python 会在标准输出上打印:

#!/bin/sh
python recordSound.py >> /tmp/my_python_log.txt

will add lines each time python outputs stuff. Go to the path specified to read the result. If you want a live view, use pipes '|' to redirect the output.

每次python输出内容时都会添加行。转到指定的路径读取结果。如果您想要实时视图,请使用管道“|” 重定向输出。

Edit : for a live view, you can also use the "tail -f /tmp/my_python_log.txt" command on another terminal as the other solution is running, which will first print the last 10 lines or so in the file, then automatically add new lines added. I love this command, really handy to view activity on a small Apache server. You can even watch multiple logs at once !

编辑:对于实时视图,您还可以在另一个终端上使用“tail -f /tmp/my_python_log.txt”命令,因为另一个解决方案正在运行,它将首先打印文件中的最后 10 行左右,然后自动打印添加新行。我喜欢这个命令,非常方便查看小型 Apache 服务器上的活动。您甚至可以一次观看多个日志!

回答by Eero Aaltonen

A very easy way would to redirect output from your script to a file

一种非常简单的方法是将脚本的输出重定向到文件

#!/bin/sh
python recordSound.py >> logfile.log

See the docs on IO redirection

请参阅有关IO 重定向的文档