I have a long-running process with stdout redirected to a file. E.g.:
./my-script.sh > file.txt &
Part of the stdout is still cached, but I would like to flush it to the file, to see the results earlier. Is there a way to do it?
Use the fflush Function to Flush stdout Output Stream in C C standard library provides an I/O library, stdio, that essentially represents a buffered version of I/O operations done in userspace, thus improving performance for common use-cases.
This is because calling sys.stdout.flush () forces it to “flush” the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so. The sys module provides functions and variables used to manipulate different parts of the Python runtime environment.
If the user needs to force writing to kernel buffers, it needs to flush the given stream provided by the fflush function. fflush takes a single argument of FILE pointer to the given stream. Note that fflush forces write function for output streams while discarding any buffered data for input streams (with seekable files).
If you want to reroute this output, you can start a tee process, symlink the stdout of the process you're watching to a the stdin of the new process. You can reroute basically anything you want this way.
The caching is handled by the libc. You can use the stdbuf
command to change the buffer size:
stdbuf -o0 ./my-script.sh > file.txt &
-o0
sets the buffer size for stdout to 0
. Probably you also want -e0
for stderr.
You can inspect the /proc/
filesystem and alter the file descriptor of stdout
. For example:
gerard@droole ~$ bash -c '
while [ true ]; do echo "."; sleep .5; done
' > ./myfile.txt &
[1] 3816
gerard@droole ~$ ls -la /proc/3816/fd/1
l-wx------ 1 gerard gerard 64 May 30 14:55 /proc/3816/fd/1 -> /home/gerard/myfile.txt
You can see that stdout is symlinked to the file I specified on the command line. If you want to change it, you can simply link it to something else.
If you want to reroute this output, you can start a tee
process, symlink the stdout of the process you're watching to a the stdin of the new process. You can reroute basically anything you want this way.
However, this is not very stable, as your programs output will be broken if you do not carefully restore its stdout file descriptor before the tee
process is terminated.
But it is not impossible ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With