Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to flush stdout of a running process

Tags:

linux

bash

stdout

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?

like image 558
W.F. Avatar asked May 30 '15 12:05

W.F.


People also ask

How to flush stdout output stream in C?

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.

What does SYS stdout flush do in Python?

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.

How to force writing to kernel buffers using fflush?

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).

How to re-route output to another process?

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.


2 Answers

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.

like image 64
hek2mgl Avatar answered Nov 16 '22 02:11

hek2mgl


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 ;)

like image 35
Gerard van Helden Avatar answered Nov 16 '22 02:11

Gerard van Helden