Bourne Shell 中的刷新输出
我在 Upstart 脚本中使用 echo 来记录事情:
script
echo "main: some data" >> log
end script
post-start script
echo "post-start: another data" >> log
end script
现在这两个并行运行,所以在日志中我经常看到:
main: post-start: some data another data
这并不重要,所以我不会采用适当的同步,但我想我会将自动刷新打开到 at至少减少这种影响。 有没有简单的方法可以做到这一点?
更新:是的,冲洗不能正确修复它,但我已经看到它在某种程度上有助于这种情况,这就是我在这种情况下所需要的。 只是不知道如何在Shell中实现
I use echo in Upstart scripts to log things:
script
echo "main: some data" >> log
end script
post-start script
echo "post-start: another data" >> log
end script
Now these two run in parallel, so in the logs I often see:
main: post-start: some data another data
This is not critical, so I won't employ proper synching, but thought I'd turn auto flush ON to at least reduce this effect. Is there an easy way to do that?
Update: yes, flushing will not properly fix it, but I've seen it help such situations to some degree, and this is all I need in this case. It's just that I don't know how to do it in Shell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试更改:
至:
Try changing:
To:
我通常使用尾随
|grep -F --line-buffered ''
作为强制行缓冲的廉价且可靠的方法。 还有 moreutils 的sponge
可以延迟所有输出,直到输入完成并关闭。I usually use trailing
|grep -F --line-buffered ''
as a cheap and reliable way to enforce line buffering. There's alsosponge
from moreutils to delay all output until input is finished and closed.为什么你认为冲洗会有帮助? echo 完成的写入包含换行符,因此如果第一个脚本在第二个脚本之前运行完成,则换行符已经在那里。
在输出中并非如此,这表明第二个脚本是在第一个脚本完成之前运行的,因此它们的输出是交错的。
冲洗对此没有帮助,这是一个“适当的”并行竞争条件。
Why do you assume that flushing would help? The write done by echo includes a newline, so if the first script ran to completion before the second, the newline would already by there.
In the output it isn't, which indicates that the second script was run before the first was complete, thus interleaving their outputs.
Flushing won't help with this, it's a "proper" parallel race condition.