Unix: How to redirect stderr to stdout

本文介绍了在Unix环境中如何将标准错误输出(stderr)重定向到标准输出(stdout),以方便查看和处理程序错误信息及堆栈跟踪。通过示例展示了如何使用重定向操作符将Java程序的异常信息整合显示,并说明了如何将这些输出分别记录到不同的文件中。

Unix: How to redirect stderr to stdout | Geekality

Unix: How to redirect stderr to stdout

Today I ran a Java application in a Unix console. It printed out some messages before it crashed with an exception and a loong stack trace. Too long to see the top of. So, I figured I’d just pipeline it into the lovely less like this:

$ java -jar foobar.jar | less

The result was not what I expected however. All I got to see was the messages that had been printed out. No exception or stack trace to be seen.

 

The reason for this is quite simply that a Unix process has three different streams: stdin(0), stdout(1) and stderr(2). When we pipe output from one process into another we sort of connect stdout of the first one to stdin of the second.

So, to be able to see my giant stack trace, we can for example redirect stderr to stdout. This way we will get both streams into stdin of the next process.

$ java -jar foobar.jar 2 >& 1 | less

Fancy, huh? We can also direct these streams into files, which might come in handy.

$ java -jar foobar.jar 1 >out.log 2 >err.log

If you want the output to be appended to the log files instead of replacing them, you can do adjust the command to this:

$ java -jar foobar.jar 1 >>out.log 2 >>err.log

:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值