Linux 如何在将标准输出重定向到 Bash 中的文件时添加时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21098382/
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
How to add timestamp while redirecting stdout to file in Bash?
提问by wondra
I have a program (server) and I am looking for a way (script) that will redirect (or better duplicate) all its stdout
to file and add timestamp for each entry.
我有一个程序(服务器),我正在寻找一种方法(脚本),可以将其全部重定向(或更好地复制)stdout
到文件并为每个条目添加时间戳。
I've done some research and the furthest I could get was thanks to How to add timestamp to STDERR redirection. It redirects stdout
but the timestamp added is of the time when the script finishes:
我做了一些研究,我能得到的最大的收获是感谢How to add timestamp to STDERR redirection。它重定向stdout
但添加的时间戳是脚本完成的时间:
#!/bin/bash
./server | ./predate.sh > log.txt
code of predate.sh
:
代码predate.sh
:
#!/bin/bash
while read line ; do
echo "$(date): ${line}"
done
It seems that server output is flushed after exit of the program.(without redirecting it works fine). Also if I try using predate.sh
on given example in mentioned thread, it works perfectly. I am aware it would be easy adding a timestamp to the main program but I would rather avoid editing its code.
似乎服务器输出在程序退出后被刷新。(没有重定向它工作正常)。此外,如果我尝试predate.sh
在提到的线程中使用给定的示例,它可以完美运行。我知道向主程序添加时间戳很容易,但我宁愿避免编辑其代码。
回答by HelpBox
If I understand your problem is to have stderr output included in your log.txt file. Right ? If that's what you want the solution is:
如果我理解您的问题是在 log.txt 文件中包含 stderr 输出。对 ?如果那是您想要的解决方案是:
./server 2>&1 | ./predate.sh > log.txt
./服务器 2>&1 | ./predate.sh > log.txt
Regards
问候
回答by Ashish
For Me Your Code is working perfectly fine
对我来说,您的代码运行良好
Check this is what I tried
检查这是我试过的
test.sh
测试文件
#!/bin/bash
while true; do
echo "hello"
done
predate.sh
早于.sh
#!/bin/bash
while read line; do
echo $(date) ":" $line;
done
then
然后
./test.sh | ./predate.sh
gives me
给我
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
Tue Jan 14 17:49:47 IST 2014 : hello
This can be redirected to some file using ">" or ">>" for append
这可以使用“>”或“>>”进行附加重定向到某个文件
回答by oliver
I recently needed exactly that: receive log messages in a serial console (picocom), print them to a terminal and to a file AND prepend the date.
我最近正好需要:在串行控制台 (picocom) 中接收日志消息,将它们打印到终端和文件,并在前面加上日期。
What I now use looks s.th. like this:
我现在使用的看起来很糟糕。像这样:
picocom -b 115200 /dev/tty.usbserial-1a122C | awk '{ print strftime("%s: "), #!/bin/bash
exec > >(ts>>file.log)
echo hello 1
echo hello 2
sleep 5
echo hello 3
; fflush(); }' | tee serial.txt
- the output of
picocom
is piped toawk
awk
prepends the date (the%s
option converts the time to the Number of seconds since 1970-01-01 00:00:00 UTC- or use%c
for a human-readable format)fflush()
flushes any buffered outputinawk
- that is piped to
tee
which diverts it to a file. (you can find some stuff abouttee
here)
回答by nyet
Again, using ts
from moreutils
, you can just use exec at the top of your script.
同样,使用ts
from moreutils
,您可以在脚本顶部使用 exec 。