如何从已经ALREADY运行的进程捕获stdout [英] How can I capture the stdout from a process that is ALREADY running

查看:314
本文介绍了如何从已经ALREADY运行的进程捕获stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行cron工作,将要一段时间,我想查看其stdout。我不知道这个过程是由cron启动的事实是多么重要,但我想我会提到它。这是在OSX所以,我没有访问像... / proc / [pid] / ...,或truss或strace的东西。使用IO重定向执行的建议(例如 script> output& tail -f output )不可接受,因为此进程1)已在运行,以及2) t重定向停止/重新启动。如果有通用的解决方案,将工作在各个Unices,这将是理想的,但具体来说,我想在Mac上完成这个现在。

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.

推荐答案

OSX的真正解决方案



将以下函数写入您的〜/ .bashrc 〜/ .zshrc

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

用法:

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

注意

您必须在El Capitan或更高版本上禁用 dtrace 限制。

You must disable dtrace restriction on El Capitan or later.

csrutil enable --without dtrace

这篇关于如何从已经ALREADY运行的进程捕获stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆