在 bash 脚本仍在运行时强制将输出刷新到文件

问题描述:

我有一个小脚本,它每天由 crontab 使用以下命令调用:

I have a small script, which is called daily by crontab using the following command:

/homedir/MyScript &> some_log.log

此方法的问题在于 some_log.log 仅在 MyScript 完成后创建.我想在程序运行时将程序的输出刷新到文件中,以便我可以执行诸如

The problem with this method is that some_log.log is only created after MyScript finishes. I would like to flush the output of the program into the file while it's running so I could do things like

tail -f some_log.log

并跟踪进度等

bash 本身实际上永远不会将任何输出写入您的日志文件.相反,它作为脚本的一部分调用的命令将每个单独写入输出并在需要时刷新.所以你的问题实际上是如何强制 bash 脚本中的命令刷新,这取决于它们是什么.

bash itself will never actually write any output to your log file. Instead, the commands it invokes as part of the script will each individually write output and flush whenever they feel like it. So your question is really how to force the commands within the bash script to flush, and that depends on what they are.