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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:50:30  来源:igfitidea点击:

How to add timestamp while redirecting stdout to file in Bash?

linuxbashredirect

提问by wondra

I have a program (server) and I am looking for a way (script) that will redirect (or better duplicate) all its stdoutto 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 stdoutbut 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.shon 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

这可以使用“>”或“>>”进行附加重定向到某个文件

Check Snapshot

检查快照

回答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 picocomis piped to awk
  • awkprepends the date (the %soption converts the time to the Number of seconds since 1970-01-01 00:00:00 UTC- or use %cfor a human-readable format)
  • fflush()flushes any buffered outputin awk
  • that is piped to teewhich diverts it to a file. (you can find some stuff about teehere)
  • 的输出通过picocom管道传输到awk
  • awk前置日期(该%s选项将时间转换为自 1970-01-01 00:00:00 UTC 以来的秒数- 或%c用于人类可读的格式)
  • fflush()刷新所有缓冲的输出awk
  • 通过管道将tee其转移到文件中。(你可以tee在这里找到一些东西)

回答by nyet

Again, using tsfrom moreutils, you can just use exec at the top of your script.

同样,使用tsfrom moreutils,您可以在脚本顶部使用 exec 。

##代码##