如何将时间戳添加到bash脚本日志?

我有一个不断运行的脚本,我输出到一个日志文件:

script.sh >> /var/log/logfile 

我想在每个附加到日志的行之前添加一个时间戳。 喜欢:

 Sat Sep 10 21:33:06 UTC 2011 The server has booted up. Hmmph. 

有什么可以使用的柔术吗?

    您可以通过以当前date和时间为前缀的循环来pipe理脚本的输出:

     ./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile 

    如果你会使用这个,很容易做一个bash函数来处理这个循环:

     adddate() { while IFS= read -r line; do echo "$(date) $line" done } ./thisscript.sh | adddate >>/var/log/logfile ./thatscript.sh | adddate >>/var/log/logfile ./theotherscript.sh | adddate >>/var/log/logfile 

    请参阅Ubuntu“moreutils”pkg中的“ts”:

     command | ts 

    或者,如果$命令执行自动缓冲(需要expect-dev pkg):

     unbuffer command | ts 

    您可以简单地将命令输出显到日志文件。 即

     echo "`date -u` `./script.sh`" >> /var/log/logfile 

    它真的工作:)

    例:

     [sparx@E1]$ ./script.sh Hello Worldy [sparx@E1]$ echo "`date -u` `./script.sh`" >> logfile.txt [sparx@E1]$ cat logfile.txt Mon Sep 12 20:18:28 UTC 2011 Hello Worldy [sparx@E1]$ 

    date命令将提供该信息

     date -u Sat Sep 10 22:39:24 UTC 2011 

    所以你可以

     echo $(date -u) "Some message or other" 

    那是你想要的吗?

    创build一个config.sh文件

     #!/usr/bin/env bash LOGFILE="/path/to/log.log" TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"` 

    当你需要发送到日志文件使用

     #!/usr/bin/env bash source /path/to/config.sh echo "$TIMESTAMP Say what you are doing" >> $LOGFILE do_what_you_want >> $LOGFILE 

    日志文件看起来像

     2013-02-03 18:22:30 Say what you are doing 

    所以按datesorting很容易

    你的意思是:

     (date && script.sh) >> /var/log/logfile 

    接受的答案https://serverfault.com/a/310104可能会有点慢,如果需要处理大量的行,开始date过程的开销,允许在Ubuntu每秒约50行,并只有约10-20在Cygwin。

    bash可以被假设时,一个更快的select将是printf内build的%(...)T格式说明符。 比较

     >> while true; do date; done | uniq -c 47 Wed Nov 9 23:17:18 STD 2016 56 Wed Nov 9 23:17:19 STD 2016 55 Wed Nov 9 23:17:20 STD 2016 51 Wed Nov 9 23:17:21 STD 2016 50 Wed Nov 9 23:17:22 STD 2016 >> while true; do printf '%(%F %T)T\n'; done | uniq -c 20300 2016-11-09 23:17:56 31767 2016-11-09 23:17:57 32109 2016-11-09 23:17:58 31036 2016-11-09 23:17:59 30714 2016-11-09 23:18:00 

    pipe一个“sed”:

     script.sh | sed "s|^|$('date') :: |" >> /var/log/logfile 

    尝试这个

     timestamp() { date +"%Y-%m-%d %T" } 

    在每个回显命令中调用这个时间戳函数:

     echo "$(timestamp): write your log here" >> /var/log/<logfile>.log 

    你可能会注意到awk运行速度很快,

     gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' yes |head -5000000 |gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' |uniq -c 461592 [2017-02-28 19:46:44] y 488555 [2017-02-28 19:46:45] y 491205 [2017-02-28 19:46:46] y 498568 [2017-02-28 19:46:47] y 502605 [2017-02-28 19:46:48] y 494048 [2017-02-28 19:46:49] y 493299 [2017-02-28 19:46:50] y 498005 [2017-02-28 19:46:51] y 502916 [2017-02-28 19:46:52] y 495550 [2017-02-28 19:46:53] y 73657 [2017-02-28 19:46:54] y 

    但是,sed运行得更快,

     sed -e "s/^/$(date -R) /" yes |head -5000000 |sed -e "s/^/$(date -R) /" |uniq -c 5000000 Tue, 28 Feb 2017 19:57:00 -0500 y 

    但是,仔细观察,似乎并没有改变时间,

     vmstat 1 | sed -e "s/^/$(date -R) /" 

    该脚本在terminal输出输出,并保存在日志文件中。

     #!/bin/bash MY_LOG=/var/log/output.log echolog(){ if [ $# -eq 0 ] then cat - | while read -r message do echo "$(date +"[%F %T %Z] -") $message" | tee -a $MY_LOG done else echo -n "$(date +'[%F %T %Z]') - " | tee -a $MY_LOG echo $* | tee -a $MY_LOG fi } echolog "My script is starting" whoami | echolog 

    示例输出:

     [2017-10-29 19:46:36 UTC] - My script is starting [2017-10-29 19:46:36 UTC] - root 

    以下是我的日志文件内容

     xiongyu@ubuntu:~/search_start_sh$ tail restart_scrape.log 2017-08-25 21:10:09 scrape_yy_news_main.py got down, now I will restart it 2017-08-25 21:10:09 check_yy_news_warn.py got down, now I will restart it 2017-08-25 21:14:53 scrape_yy_news_main.py got down, now I will restart it 

    我的一些shell内容如下

     log_file="restart_scrape.log" TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"` echo "$TIMESTAMP $search_py_file got down, now I will restart it" | tee -a $log_file