如何在 Mac 上记录没有缓冲的后台进程的 STDOUT?

发布于 2024-09-26 01:38:26 字数 564 浏览 1 评论 0原文

我正在 Mac 上运行后台进程,并且遇到日志更新问题。如果我运行

someprog > mylog &

,那么 mylog 不会立即更新,而是每隔一定时间更新 - 我猜这是由于缓冲所致。 现在也是如此。如果我在输出写入 mylog 之前终止程序,那么我就会丢失数据。同样的程序在 Linux 机器上没有这样的问题,所以我希望我也能在 Mac 上对其进行运行时更新。知道怎么做吗?

someprog 是一个F77程序,不是我写的。

试图在 SuperUser 上问这个问题,但没有人可以帮助我。

EDIT1:我不想更改来源,但请记住这一点。日志记录在 Linux 机器上运行良好,因此它也应该在 Mac 上运行。它一定是系统设置,例如缓冲区大小?对我来说,将缓冲区大小限制为较小的值就好了——现在我必须等待几个小时才能看到日志中的内容。

I am running a background process on Mac and have a problem with log update. If I run

someprog > mylog &

then mylog is updated not immediately, but with some intervals - I guess it's due to buffering. Same thing with at now. If I kill the program before output is written to mylog, then I loose the data. There was no such problem with the same program on Linux machines, so I hope I can make it run-time-updated on Mac as well. Any idea how?

someprog is a F77 program, which was not written by me.

I tried to ask this question at SuperUser, but no one can help me there.

EDIT1: I don't feel like changing the source, but keep it in mind. Logging works fine on Linux machines, so it should work on Macs as well. It must be a system setting, e.g. buffer size? It would be fine for me to limit the buffer size to a smaller value - now I have to wait hours to see something in the log.

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

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

发布评论

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

评论(2

谈情不如逗狗 2024-10-03 01:38:26

如果您有权访问源代码,您可能只需在每个 printf 之后添加对 fflush(stdout) 的调用即可。如果你不这样做,你可以尝试使用 LD_PRELOAD 进行一些棘手的操作...基本上,制作你自己的 printf() 版本,它调用 libc 的 printf...并进行刷新...然后在运行时 LD_PRELOAD 该库...该应用程序将使用您的......虽然有点冒险......

If you have access to the source code, you can probably just add calls to fflush(stdout) after every printf. If you don't, you could try something tricky with LD_PRELOAD... Basically, make your own version of printf() that calls libc's printf... AND does a flush... Then LD_PRELOAD that library when you run... The app will use yours instead... Kinda risky tho...

送君千里 2024-10-03 01:38:26

这是写入非 tty 流的 POSIX C 程序的常见行为 - 我猜 F77 具有相同的行为,或者是根据 stdio 例程编写的。

我不知道正确的答案是什么 - 我猜你需要通过假装是 tty 的东西通过管道传输输出,但我不知道什么(如果有)实用程序提供了该选项。

That's the usual behaviour of POSIX C programs that are writing to a non-tty stream - I guess F77 shares the same behaviour, or is written in terms of the stdio routines.

I don't know what the right answer is - I guess you'll need to pipe the output through something that pretends to be a tty, but offhand I don't know what (if any) utility provides that option.

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