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

theraccoonbear picture theraccoonbear · Aug 6, 2010 · Viewed 21.4k times · Source

I have a running cron job that will be going for a while and I'd like to view its stdout. I don't know how important the fact that the process was started by cron is, but I figure I'd mention it. This is on OSX so, I don't have access to things like... /proc/[pid]/..., or truss, or strace. Suggestions of executing with IO redirection (e.g. script > output & tail -f output) are NOT acceptable, because this process is 1) already running, and 2) can't be stopped/restarted with redirection. If there are general solutions that will work across various Unices, that'd be ideal, but specifically I'm trying to accomplish this on a Mac right now.

Answer

mpyw picture mpyw · Nov 21, 2015

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