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
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页面)