Bourne Shell 中的刷新输出

发布于 2024-07-14 16:03:09 字数 450 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離人涙 2024-07-21 16:03:09

尝试更改:

echo "text"

至:

cat << EOF
text
EOF

Try changing:

echo "text"

To:

cat << EOF
text
EOF
盗心人 2024-07-21 16:03:09

我通常使用尾随 |grep -F --line-buffered '' 作为强制行缓冲的廉价且可靠的方法。 还有 moreutilssponge 可以延迟所有输出,直到输入完成并关闭。

I usually use trailing |grep -F --line-buffered '' as a cheap and reliable way to enforce line buffering. There's also sponge from moreutils to delay all output until input is finished and closed.

埋葬我深情 2024-07-21 16:03:09

为什么你认为冲洗会有帮助? 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文