LoginSignup
18
19

More than 5 years have passed since last update.

OSXで既存プロセスの標準出力をキャプチャする

Last updated at Posted at 2015-11-21
~/.bashrc, ~/.zshrc などに記述
capture() {
    sudo dtrace -p "$1" -qn '
        syscall::write*:entry
        /pid == $target && arg0 == 1/ {
            printf("%s", copyinstr(arg1, arg2));
        }
    '
}
capture-all() {
    sudo dtrace -p "$1" -qn '
        syscall::write*:entry
        /pid == $target && (arg0 == 1 || arg0 == 2)/ {
            printf("%s", copyinstr(arg1, arg2));
        }
    '
}
使用例
example@localhost:~$ php -r 'while (true) { echo "Hello\n"; sleep(1); }' >/dev/null &
[1] 97755
example@localhost:~$ capture 97755
Hello
Hello
Hello
Hello
...

追記

El Capitan 以降の場合はリカバリモードで csrutil enable --without dtrace の設定が必要です。

参考

18
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19