我在那里试图使用bash创建日志,但是我遇到的时间错了。我在背景中运行此操作。

问题描述 投票:0回答:2
Fri Aug 21 01:43:12 UTC 2020 Yay I was started Fri Aug 21 01:43:12 UTC 2020 Yay I was the response returned by the url

这里的URL
https://example.com/process
睡2秒钟,但是您可以看到,执行时的时间是相同的,在我们得到响应之后。

纠正响应

Fri Aug 21 01:43:12 UTC 2020 Yay I was started Fri Aug 21 01:43:14 UTC 2020 Yay I was the response returned by the url

我怎么能解决这个问题?
    

您的问题是因为
sed "s/^/$(date -u) /"

在启动时捕获日期字符串并将其放置在每行开始时。从

curl

的响应中为每个传入的新行更新日期字符串。
使用
awk

而不是

sed
bash timestamp stdin
2个回答
5
投票
curl https://example.com/process | awk '{ printf("%s %s\n", strftime("%c", systime()), $0) }' >>/path/to/logs.log &

,以便将日期预先到每行的日期,因为它将捕获每行的新日期。

您的原始日期格式是当地时间格式的语言环境格式。如果是您的偏爱,它一直在这里被保存在这里;但是通常使用
ISO.org的日志时间戳更好地打印:ISO 8601格式或UTC相对Unix unixtimestamp.
curl https://example.com/process |
  awk '{ printf("%s %s\n", strftime("%Y-%m-%dT%H:%M:%S%z", systime()), $0) }' \
    >>/path/to/logs.log &

具有ISO-8601时间戳:

man strftime
请参阅时间格式化。
    

Moreutils提供的是您想要的。它用时间戳预启动

stdin
的每一行。

curl https://example.com/process | ts

恢复:

Mar 15 21:22:12 <!doctype html>
Mar 15 21:22:12 <html>
Mar 15 21:22:12 <head>
Mar 15 21:22:12     <title>Example Domain</title>
...

您也可以操纵时间戳格式(请参阅MAN页面)

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.