我想将脚本中的错误捕获到文件而不是屏幕中.
在*nix中,通常使用stderr重定向
echo "Error" 2> errorfile.log
Run Code Online (Sandbox Code Playgroud)
如何在Windows下的CMD脚本中执行此操作?
我想拆分,stdout以便打印到stdout和stderr.这听起来像是一份工作,tee但语法却在逃避我 -
./script.sh | tee stderr
Run Code Online (Sandbox Code Playgroud)
当然,stderr这里应该如何引用?
我想将python脚本的子进程'stdout和stdin指向同一个文件.我不知道的是如何使两个来源的线条区分开来?(例如,带有感叹号的stderr行前缀.)
在我的特定情况下,不需要对子进程进行实时监视,执行的Python脚本可以等待其执行结束.
在将程序作为systemd服务运行时,我无法将STDOUT和STDERR传送到文件.我已经尝试将以下内容添加到.service文件中:
ExecStart=/apppath/appname > /filepath/filename 2>&1
Run Code Online (Sandbox Code Playgroud)
但这不起作用.输出结果在/ var/log/messages中,可以使用journalctl查看,但我想要一个单独的文件.
我也试过设置,StdOutput=tty但找不到将其重定向到文件的方法.
任何帮助,将不胜感激.
它们之间有什么区别?它们是如何使用的?谁能指点我的例子?
具体来说,如何在两种情况下"写入"流,以及如何恢复和输出(即屏幕)已写入文本的文本?
另外,"屏幕"输出本身就是一个流吗?也许我不太了解溪流.我知道,这当然也可以保存到文件中.所有这些都会使用fprintf/ fscanf等吗?
(Gradle 3.2.1)我运行了一些java测试,它们在Stderr/Stdout中记录输出.如果我开始,我可以看到输出
gradle test --info
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,第三方图书馆的大量不需要的输出也是如此.
文档建议使用logging.caputureStandardError / logging.caputureStandardError (loglevel),但似乎没有任何影响.
tasks.withType(Test) {
logging.captureStandardOutput LogLevel.QUIET
logging.captureStandardError LogLevel.QUIET
}
Run Code Online (Sandbox Code Playgroud)
然后如果运行gradle test,则不在控制台中输出STDERR/STDOUT.
如何从控制台中的测试类中获取输出?
我有一个python子进程,我正在尝试从中读取输出和错误流.目前我有它的工作,但我只能在读完stderr之后阅读stdout.这是它的样子:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_iterator = iter(process.stdout.readline, b"")
stderr_iterator = iter(process.stderr.readline, b"")
for line in stdout_iterator:
# Do stuff with line
print line
for line in stderr_iterator:
# Do stuff with line
print line
Run Code Online (Sandbox Code Playgroud)
如您所见,stderrfor循环在stdout循环完成之前无法启动.如何修改它以便能够以正确的顺序读取这两行?
澄清:我仍然需要能够判断一条线是否来自stdout或者stderr因为我的代码中它们的处理方式不同.
NB.我已经看到了multiprocessing.Process的日志输出 - 遗憾的是,它没有回答这个问题.
我正在通过多处理创建一个子进程(在Windows上).我希望将所有子进程的stdout和stderr输出重定向到日志文件,而不是出现在控制台上.我看到的唯一建议是子进程将sys.stdout设置为文件.但是,由于Windows上的stdout重定向行为,这不能有效地重定向所有stdout输出.
要说明此问题,请使用以下代码构建Windows DLL
#include <iostream>
extern "C"
{
__declspec(dllexport) void writeToStdOut()
{
std::cout << "Writing to STDOUT from test DLL" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
然后创建并运行如下所示的python脚本,它导入此DLL并调用该函数:
from ctypes import *
import sys
print
print "Writing to STDOUT from python, before redirect"
print
sys.stdout = open("stdout_redirect_log.txt", "w")
print "Writing to STDOUT from python, after redirect"
testdll = CDLL("Release/stdout_test.dll")
testdll.writeToStdOut()
Run Code Online (Sandbox Code Playgroud)
为了看到与我相同的行为,可能需要针对与Python使用的不同的C运行时构建DLL.在我的例子中,python是使用Visual Studio 2010构建的,但我的DLL是使用VS 2005构建的.
我看到的行为是控制台显示:
> stdout_test.py
Writing to STDOUT from python, before redirect
Writing to STDOUT …Run Code Online (Sandbox Code Playgroud) 我想将脚本printf中的a 的输出bash指向stderr而不是stdout.
我不是要求重定向stderr或stdout从哪里重定向到当前路由.我只是希望能够将输出从a发送printf到stderr而不是默认值stdout.
我进行了一些实验,发现如下例所示,附加1>&2到printf我想做的事情.但是,我没有使用bash的经验.所以我的主要问题是,是否有" 更好 "的方法来做到这一点bash?
通过" 更好 "我的意思是有另一种方法来做这个更常用,更传统,或更惯用?一个更有经验的bash程序员将如何做到这一点?
#!/bin/bash
printf "{%s} This should go to stderr.\n" "$(date)" 1>&2
printf "[(%s)] This should go to stdout.\n" "$(date)"
Run Code Online (Sandbox Code Playgroud)
我还有一个次要问题.我问的不是因为我需要知道,而是因为我只是好奇并希望更好地了解正在发生的事情.
看来上面只有在shell脚本中运行时才会起作用.当我从命令行尝试它时似乎不起作用.
这是我的意思的一个例子.
irrational@VBx64:~$ printf "{%s} Sent to stderr.\n" "$(date)" 1>&2 2> errors.txt
{Sat Jun 9 14:08:46 EDT 2012} …Run Code Online (Sandbox Code Playgroud)