Skip to content

Instantly share code, notes, and snippets.

@jimklimov
Created July 30, 2026 17:21
Show Gist options
  • Select an option

  • Save jimklimov/1d096e66cd126cef246ce96aba30bbd5 to your computer and use it in GitHub Desktop.

Select an option

Save jimklimov/1d096e66cd126cef246ce96aba30bbd5 to your computer and use it in GitHub Desktop.
Force flush of other program's output buffers

Kudos to https://www.reddit.com/r/linux/comments/1y8qg2/can_i_force_a_process_to_flush_a_file_descriptor/

Re-confirmed the trick with Ubuntu 26.04 - answer is still relevant, thanks!

FWIW, reproduced with a simple program that prints a message without \n so it is not flushed until the process is closed (or buffer fills up), and sleeps:

// ppp.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

int main() {
        printf("Test message");
        sleep(60);
}

Built simply with gcc -o ppp ppp.c and started with ./ppp & The gdb test required sudo for some reason, but otherwise worked:

$ ps
    PID TTY          TIME CMD
 559772 pts/10   00:00:00 bash
 560050 pts/10   00:00:00 ppp
 560078 pts/10   00:00:00 ps
$  sudo gdb -p 560050 -batch -ex 'call fflush(0)'
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/x86_64-linux-gnu/libthread_db.so.1".
__internal_syscall_cancel (a1=a1@entry=0, a2=a2@entry=0, a3=a3@entry=140724059109536, a4=a4@entry=140724059109536, a5=a5@entry=0, a6=a6@entry=0, nr=230) at ./nptl/cancellation.c:44
warning: 44     ./nptl/cancellation.c: No such file or directory
Test message$1 = 0

The "Test message" part is what got flushed out :)

$ fg
./ppp
# Slept out the rest of the minute and exited

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment