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

后端 未结 5 902
深忆病人
深忆病人 2021-01-30 22:21

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 f

相关标签:
5条回答
  • 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
    
    0 讨论(0)
  • 2021-01-30 22:42

    DISCLAIMER: No clue if Mac has this. This technique exists on Linux. YMMV.

    You can grab stdout/err from /proc (assuming proper privileges):

    PID=$(pidof my_process)
    tail -f /proc/$PID/fd/1
    

    Or grab everything remaining in the buffer to a file:

    cat /proc/$PID/fd/1
    

    PS: fd/1 is stdout, fd/2 is stderr.


    EDIT: Alex brown> Mac does not have this, but it's a useful tip for Linux.

    0 讨论(0)
  • 2021-01-30 22:42

    neercs has the ability to "grab" programs that were started outside it. Perhaps it will work for you. BTW, you don't have truss or strace, but you do have dtrace.

    0 讨论(0)
  • 2021-01-30 22:45

    use dtruss -p <PID>, or even rwsnoop -p <PID>.

    0 讨论(0)
  • 2021-01-30 22:47

    I think the fact you started with cron could save you. Under linux any standard output of a cron job is mailed to the unix mail account of the user who owns the job. Not sure about OSX though. Unfortunately you will have to wait for the the job to finish before the mail is sent and you can view the output.

    0 讨论(0)
提交回复
热议问题