Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

bash - BASH将时间戳添加到stdout和stderr(BASH Prepend timestamp to stdout and stderr)

I am trying to prepend a timestamp to all lines in stdout and stderr.

(我正在为stdout和stderr中的所有行添加时间戳。)

However I do not want to redirect either of them to a file.

(但是,我不想将它们中的任何一个重定向到文件。)

This command is being run in a script by a process manager (pm2) which will then log stdout and stderr to their respective file.

(进程管理器(pm2)正在脚本中运行此命令,然后它将stdout和stderr记录到它们各自的文件中。)

I am trying to modify the stream (without combining them) and insert the timestamp before is logged by pm2.

(我正在尝试修改流(不合并它们)并在pm2记录之前插入时间戳。)

I can get either of them working in isolation but run into issues modifying both.

(我可以让他们两个都孤立地工作,但是遇到修改两者的问题。)

I appreciate any assistance you can provide.

(感谢您提供的任何帮助。)

Here is my somewhat working code

(这是我的一些工作代码)

python3.6 ./bot.py > >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }') 2> >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }')
  ask by scorch855 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to redirect output from the second gawk command back to stderr:

(您需要将第二个gawk命令的输出重定向回stderr:)

python3.6 ./bot.py 
    > >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }') 
   2> >(gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' >&2)
#                                             this part here ^^^

Without that, the second gawk command is taking input from python3.6 's stderr, but sending output to the same stdout as everything else.

(python3.6 ,第二个gawk命令将从python3.6的stderr接收输入,但将输出发送到与其他所有命令相同的stdout。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...