将多个标准输出重定向到单个文件

发布于 2024-08-30 02:12:39 字数 151 浏览 11 评论 0原文

我有一个程序在多台带有 NFS 的机器上运行,我想将它们的所有输出记录到一个文件中。我可以运行 ./my_program >>每台机器上的 filename 或者是否存在我应该注意的并发问题?因为我只是追加,所以我认为不会有问题,但我只是想确定一下。

I have a program running on multiple machines with NFS and I'd like to log all their outputs into a single file. Can I just run ./my_program >> filename on every machine or is there an issue with concurrency I should be aware of? Since I'm only appending, I don't think there would be a problem, but I'm just trying to make sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半透明的墙 2024-09-06 02:12:39

这可能可行,但是,是的,您将遇到并发问题,并且日志文件基本上无法辨认。

我建议每台机器都有一个日志文件,然后定期(比如每晚)将文件与机器名称连接在一起作为文件名:

for i in "/path/to/logfiles/*"; do
    echo "Machine: $i";
    cat $i;
done > filename.log

我认为这应该会给您一些想法。

That could work, but yes, you will have concurrency issues with it, and the log file will be basically indecipherable.

What I would recommend is that there be a log file for each machine and then on some periodical basis (say nightly), concatenate the files together with the machine name as the file name:

for i in "/path/to/logfiles/*"; do
    echo "Machine: $i";
    cat $i;
done > filename.log

That should give you some ideas, I think.

嘦怹 2024-09-06 02:12:39

NFS 协议不支持原子附加写入,因此附加写入在任何平台的 NFS 上都不是原子的。如果您尝试,文件最终会损坏。

当从多个线程或进程追加到文件时,在文件以追加模式打开、写入的字符串不超过文件系统块大小并且文件系统是本地的条件下,对该文件的 fwrite 是原子的。但在 NFS 中并非如此。

有一个解决方法,尽管我不知道如何从 shellscript 中做到这一点。该技术称为接近开放的缓存一致性

The NFS protocol does not support atomic append writes, so append writes are never atomic on NFS for any platform. Files WILL end up corrupt if you try.

When appending to files from multiple threads or processes, the fwrites to that file are atomic under the condition that the file was opened in appending mode, the string written to it does not exceed the filesystem blocksize and the filesystem is local. Which in NFS is not the case.

There is a workaround, although I would not know how to do it from a shellscript. The technique is called close-to-open cache consistency

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