如何将时间戳添加到 STDERR 重定向 [英] How to add timestamp to STDERR redirection

查看:80
本文介绍了如何将时间戳添加到 STDERR 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 bash/ksh 中,我们可以为 STDERR 重定向添加时间戳吗?

In bash/ksh can we add timestamp to STDERR redirection?

例如myscript.sh 2> error.log

E.g. myscript.sh 2> error.log

我也想在日志上写一个时间戳.

I want to get a timestamp written on the log too.

推荐答案

如果您在谈论每一行的最新时间戳,那么您可能希望在实际脚本中执行此操作(但请参阅如果您无权更改它,请在下面找到一个漂亮的解决方案).如果您只想在脚本开始编写之前将标记日期放在自己的行上,我会使用:

If you're talking about an up-to-date timestamp on each line, that's something you'd probably want to do in your actual script (but see below for a nifty solution if you have no power to change it). If you just want a marker date on its own line before your script starts writing, I'd use:

( date 1>&2 ; myscript.sh ) 2>error.log

您需要的是通过另一个程序来管理 stderr 的技巧,该程序可以为每一行添加时间戳.你可以用一个 C 程序来做到这一点,但有一种更狡猾的方法,只使用 bash.

What you need is a trick to pipe stderr through another program that can add timestamps to each line. You could do this with a C program but there's a far more devious way using just bash.

首先,创建一个脚本,将时间戳添加到每一行(称为predate.sh):

First, create a script which will add the timestamp to each line (called predate.sh):

#!/bin/bash
while read line ; do
    echo "$(date): ${line}"
done

例如:

( echo a ; sleep 5 ; echo b ; sleep 2 ; echo c ) | ./predate.sh

产生:

Fri Oct  2 12:31:39 WAST 2009: a
Fri Oct  2 12:31:44 WAST 2009: b
Fri Oct  2 12:31:46 WAST 2009: c

然后你需要另一个技巧来交换标准输出和标准错误,这里的这个小怪物:

Then you need another trick that can swap stdout and stderr, this little monstrosity here:

( myscript.sh 3>&1 1>&2- 2>&3- )

然后通过给 stdout 加上时间戳并将其重定向到您的文件来组合这两个技巧很简单:

Then it's simple to combine the two tricks by timestamping stdout and redirecting it to your file:

( myscript.sh 3>&1 1>&2- 2>&3- ) | ./predate.sh >error.log

以下文字记录显示了这一点:

The following transcript shows this in action:

pax> cat predate.sh
    #!/bin/bash
    while read line ; do
        echo "$(date): ${line}"
    done
pax> cat tstdate.sh
    #!/bin/bash
    echo a to stderr then wait five seconds 1>&2
    sleep 5
    echo b to stderr then wait two seconds 1>&2
    sleep 2
    echo c to stderr 1>&2
    echo d to stdout
pax> ( ( ./tstdate.sh ) 3>&1 1>&2- 2>&3- ) | ./predate.sh >error.log
    d to stdout
pax> cat error.log
    Fri Oct  2 12:49:40 WAST 2009: a to stderr then wait five seconds
    Fri Oct  2 12:49:45 WAST 2009: b to stderr then wait two seconds
    Fri Oct  2 12:49:47 WAST 2009: c to stderr

如前所述,predate.sh 将在每一行前面加上一个时间戳,tstdate.sh 只是一个写入 stdoutstderr 具有特定的时间间隔.

As already mentioned, predate.sh will prefix each line with a timestamp and the tstdate.sh is simply a test program to write to stdout and stderr with specific time gaps.

当您运行该命令时,您实际上将 "d to stdout" 写入 stderr(但这是您的 TTY 设备或其他任何 stdout 在您开始时可能已).带时间戳的 stderr 行将写入您想要的文件.

When you run the command, you actually get "d to stdout" written to stderr (but that's your TTY device or whatever else stdout may have been when you started). The timestamped stderr lines are written to your desired file.

这篇关于如何将时间戳添加到 STDERR 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆