I found a solution to this here. Using the OP's example you basically run

stdbuf -oL /homedir/MyScript &> some_log.log

and then the buffer gets flushed after each line of output. I often combine this with nohup to run long jobs on a remote machine.

stdbuf -oL nohup /homedir/MyScript &> some_log.log

This way your process doesn't get cancelled when you log out.

Answer from Martin Wiebusch on Stack Overflow
Discussions

How do I flush stdout in a `%%script bash` cell?
How do I flush stdout in a %%script bash cell in a Jupyter notebook (Google Colab, actually)? I have some long-running script cells, and I like to see the results as they are coming out, not waiting for the whole cell to finish. More on discourse.jupyter.org
🌐 discourse.jupyter.org
0
0
January 21, 2022
What are the ways to flush a Linux command's stdout as a child process?
Node.js Version: 9.8.0 OS: Kubuntu 17.10 Scope (install, code, runtime, meta, other?): runtime Module (and version) (if relevant): child_process What are the ways to flush a Linux command's std... More on github.com
🌐 github.com
22
March 28, 2018
linux - Force a shell script to fflush - Stack Overflow
I was wondering if it was possible ... on stdout/stderr respectively? A quick and dirty solution would be to write my own printf implementation that did this and use it in lieu of either built in, but it occurred to me that I might not need to. I'm writing several build scripts that run at once, for debugging needs I really need to see messages that they write in order. ... Not that I know of, I think that BASH is completely line based so they will flush on a line ... More on stackoverflow.com
🌐 stackoverflow.com
Set flushing mode for output stream
Is there a way either 1) to specify the flushing mode of an existing output stream or 2) to create a new stream that has the desired flushing mode? [Edit: the output stream needs to be connected to the “screen” (console) and directable to a text file.] I’m still suffering from the fact ... More on discourse.julialang.org
🌐 discourse.julialang.org
0
2
August 7, 2023
🌐
Jupyter Community Forum
discourse.jupyter.org › notebook
How do I flush stdout in a `%%script bash` cell? - Notebook - Jupyter Community Forum
January 21, 2022 - How do I flush stdout in a %%script bash cell in a Jupyter notebook (Google Colab, actually)? I have some long-running script cells, and I like to see the results as they are coming out, not waiting for the whole cell t…
🌐
GitHub
github.com › nodejs › help › issues › 1187
What are the ways to flush a Linux command's stdout as a child process? · Issue #1187 · nodejs/help
March 28, 2018 - What are the ways to flush a Linux command's stdout as a Node.js child process? Ending the stdin stream (child.stdin.end) will work. As will unbuffering the command with stdbuf. But I imagine there's a proper way to stream results from external commands. How do we tell a command we're ready to consume while we are still providing data?
Author   vibl
Top answer
1 of 6
21

IF one were truly wanting that data, I'd suggest attaching the gdb debugger to the python interpreter, momentarily stopping the task, calling fsync(1) (stdout), detach from it (resuming the process) and go peruse the output file.

Look in /proc/$(pidof python)/fd to see valid file descriptors. $(pidof x) returns the PID of process named 'x'.

# your python script is running merrily over there.... with some PID you've determined.
#
# load gdb
gdb
#
# attach to python interpreter (use the number returned by $(pidof python))
attach 1234
#
# force a sync within the program's world (1 = stdout, which is redirected in your example)
call fsync(1)
#
# the call SHOULD have returned 0x0, sync successful.   If you get 0xffffffff (-1), perhaps that wasn't stdout.  0=stdin, 1=stdout, 2=stderr
#
# remove our claws from poor python
detach
#
# we're done!
quit

I've used this method to change working dir's, tweak settings on the fly... many things. Alas, you can only call functions which are defined in the running program, fsync works nicely though.

(gdb command 'info functions' will list all of the functions available. Be careful though. You're operating LIVE on a process.)

There is also the command peekfd (found in psmisc package on Debian Jessie and others) which will allow you to see what's hiding in buffers of a process. Again, /proc/$(pidof python)/fd will show you valid file descriptors to give as arguments to peekfd.

If you don't remember -u for python, you can always prefix a command with stdbuf (in coreutils, already installed) to set stdin/stdout/stderr to unbuffered, line buffered or block buffered as desired:

stdbuf -i 0 -o 0 -e 0 python myscript.py > unbuffered.output

Of course, man pages are your friends, hey! perhaps an alias might be useful here too.

alias python='python -u'

Now your python always uses -u for all your command line endeavors!

2 of 6
5

First make sure you have the debugging symbols for Python (or at least glibc). On Fedora1 you can install them with:

dnf debuginfo-install python

Then attach gdb to the running script and run the following commands:

[user@host ~]$ pidof python2
9219
[user@host ~]$ gdb python2 9219
GNU gdb (GDB) Fedora 7.7.1-13.fc20
...
0x00007fa934278780 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:81
81  T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) call fflush(stdout)
$1 = 0
(gdb) call setvbuf(stdout, 0, 2, 0)
$2 = 0
(gdb) quit
A debugging session is active.

    Inferior 1 [process 9219] will be detached.

Quit anyway? (y or n) y
Detaching from program: /usr/bin/python2, process 9219

This will flush stdout and also disable buffering. The 2 from the setvbuf call is the value of _IONBF on my system. You'll need to find out what's on yours (a grep _IONBF /usr/include/stdio.h should do the trick).

Based on what I've seen in the implementation of PyFile_SetBufSize and PyFile_WriteString in CPython 2.7, it should work pretty well, but I can't make any guarantees.


1 Fedora includes a special type of RPMs called debuginfo rpms. These automatically created RPMs contain the debugging information from the program files, but moved into an external file.

Find elsewhere
🌐
TurnKey Linux
turnkeylinux.org › blog › unix-buffering
Unix buffering delays output to stdout, ruins your day | TurnKey GNU/Linux
October 6, 2014 - sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) Output to stderr instead which is unbuffered by default, but this is a bit of an ugly hack. Changing a program to flush as needed or not buffer is practical when you are the author, but it's a bit more problematic when you just want to run an existing program without Unix buffering getting in your way.
🌐
GNU
lists.gnu.org › archive › html › bug-bash › 2008-01 › msg00017.html
Re: stdio buffer flushing on redirection
January 6, 2008 - While bash correctly takes care to flush the buffers after output in e.g. the echo builtin, it does not handle the case of output failure. In case fflush fails, under some implementations, the output may remain in the stdout buffer, and when printing something later, after redirecting stdout to some file, the previous output (which should not have gone to this file) will remain in the buffer, and be printed together with the new output into the target file.
🌐
Julia Programming Language
discourse.julialang.org › general usage
Set flushing mode for output stream - General Usage - Julia Programming Language
August 7, 2023 - Is there a way either 1) to specify the flushing mode of an existing output stream or 2) to create a new stream that has the desired flushing mode? [Edit: the output stream needs to be connected to the “screen” (console) and directable to a text file.] I’m still suffering from the fact that each time I want to print something immediately, I need to add flush(stdout) after the print statement: https://discourse.julialang.org/t/stderr-not-flushed-right-away I could use the @info facility in som...
🌐
GitHub
github.com › jupyter › notebook › issues › 6277
[FR/QST] How do I flush stdout in a `%%script bash` cell? · Issue #6277 · jupyter/notebook
January 21, 2022 - Prescript: I posted this first on the Discourse, but there is some module active there that bans IPs from VPS subnets, which I use to escape Iran's Internet filtering. I suggest using some other antispam measure. How do I flush stdout in...
Author   NightMachinery
🌐
Reddit
reddit.com › r/bash › how to get realtime output from bash
r/bash on Reddit: How to get realtime output from bash
February 13, 2024 -

Have a script that runs a custom program within bash, the exe takes a while to run, about 5 minutes, but when you run it there are heartbeats printed to let the user know that it is still running, on my bash, those messages appear after the script has finished running, also, it seems the buffer is full and not every message gets printed, I tried redirecting to a file but still the output is not "real time"

something like

# !/bin/bash
test_executable --flag=1 | tee "/tmp/test_output.txt"

this is on macos, also tried to output to /dev/tty but it says its not configured.

🌐
Google Groups
groups.google.com › g › comp.unix.shell › c › r5ptMYJFTuU
bash: how to flush the output from command `tee`
March 25, 2010 - (I am guessing that the OP would be actually running ... | tee /dev/stderr | tee /path/to/log_file | something as otherwise they will be getting two copies of the output of ..., one on stderr and one on stdout)
🌐
Unix.com
unix.com › applications › programming
stdout/stdin + flushing buffers - Programming - Unix Linux Community
September 24, 2008 - Hi all I've run into a snag in a program of mine where part of what I entered in at the start of run-time, instead of the current value within printf() is being printed out. After failing with fflush() and setbuf(), I tried the following approach void BufferFlusher() { int in=0; char c; while( (in = getchar()) != NULL) { c = (char) in; c = NULL; fputc((int)c,stdin); } } however this has not improved things (even with replacing getchar() with fgetc(std...
🌐
Julia Programming Language
discourse.julialang.org › general usage
Stderr not flushed right away? - General Usage - Julia Programming Language
August 14, 2022 - stderr doesn’t seem to flush frequently enough. Is this by design? Suppose we run this Julia script #--- try.jl --- for i = 1:100000 println(stderr, i) sleep(0.5) end from the shell command line like so $ julia try.jnl > log.txt 2>&1 & Then, monitor the progress of the script by $ tail -f log.txt When I tested this on macOS and centOS, stderr is flushed very infrequently.