How can I capture the stdout from a process that is ALREADY running

True solution for OSX

Write the following function to your ~/.bashrc or ~/.zshrc.

capture() {
    sudo dtrace -p "$1" -qn '
        syscall::write*:entry
        /pid == $target && arg0 == 1/ {
            printf("%s", copyinstr(arg1, arg2));
        }
    '
}

Usage:

example@localhost:~$ perl -e 'STDOUT->autoflush; while (1) { print "Hello\n"; sleep 1; }' >/dev/null &
[1] 97755
example@localhost:~$ capture 97755
Hello
Hello
Hello
Hello
...

https://github.com/mivok/squirrelpouch/wiki/dtrace

NOTE:

You must disable dtrace restriction on El Capitan or later.

csrutil enable --without dtrace

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)