Keep terminal output file open and appended indefinitely
This thread here discussed how to write terminal output to a file.
Suppose I run a Kafka consumer command in a terminal, using >> out.txt
to store the streamed data. Once the data stream is paused (not stopped) for some reason, the out.txt
is closed and never opened again (unless I rerun the command).
Are there any ways to keep this file open and waiting and appending indefinitely unless it is stopped manually (like with Ctrl-C)?
To clarify: this question is not about Kafka. For any command that produces indefinitely continuous output to a terminal in a non-stop manner, but for some reason, that stream of output is paused for a moment, the out.txt
is closed. That leads to the command stopping and it has to be rerun. Question: any way to keep the command waiting and appending to the same file out.txt
?
Solution 1:
Right now, the file out.txt
is already "waiting and appending indefinitely". Next time, any time, you append output to it, the file will be there and take your output.
It is the redirection symbol >>
that indicates that any data send to the file should be appended. If instead you would be using >
, then any data you send to the file would replace the contents that may have been in the file.
Just pausing a terminal command may cause the operating system to "close" the file, in a sense that it finalizes the current state of the file in the file system, but that does not close the connection of the paused command to the file. Once that command is resumed, writing to the file will be continued.
If you exit the command, or it is aborted (or crashes), both the connection of the program to the file and the file itself will be closed. When you start the command again, the connection with the file will be established, and because of the >>
symbol, additional input will be appended to the file.
I do not see the need of having something that would "stop" it receiving input. In fact, it already stopped accepting input as soon as no data is (anymore) been send. As soon as you give it new input, it will continue to accept input. Anytime.