macos 如何从已经运行的进程中捕获标准输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3425340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:07:05  来源:igfitidea点击:

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

macosprocesscronstdoutcapture

提问by theraccoonbear

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.

我有一个正在运行的 cron 作业,它将持续一段时间,我想查看它的标准输出。我不知道这个过程是由 cron 启动的这一事实有多重要,但我想我会提到它。这是在 OSX 上,所以我无法访问诸如... /proc/[pid]/...、truss 或 strace 之类的东西。使用 IO 重定向(例如script > output & tail -f output)执行的建议是不可接受的,因为此进程 1) 已经在运行,并且 2) 无法通过重定向停止/重新启动。如果有适用于各种 Unices 的通用解决方案,那将是理想的,但具体而言,我现在正试图在 Mac 上完成此任务。

回答by mpyw

True solution for OSX

OSX 的真正解决方案

Write the following function to your ~/.bashrcor ~/.zshrc.

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

capture() {
    sudo dtrace -p "" -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

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

NOTE:

笔记:

You must disable dtracerestriction on El Capitan or later.

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

csrutil enable --without dtrace

回答by Mark Renouf

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

免责声明:不知道 Mac 是否有这个。这种技术存在于 Linux 上。天啊。

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

您可以从 /proc 获取 stdout/err(假设有适当的权限):

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.

PS:fd/1 是标准输出,fd/2 是标准错误。



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

编辑:Alex brown> Mac 没有这个,但它对 Linux 来说是一个有用的提示。

回答by Alex Brown

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

使用dtruss -p <PID>, 甚至rwsnoop -p <PID>.

回答by LaC

neercshas 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.

neercs能够“抓取”在其外部启动的程序。也许它对你有用。顺便说一句,你没有 truss 或 strace,但你有 dtrace。

回答by Sodved

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.

我认为你从 cron 开始的事实可以拯救你。在 linux 下,cron 作业的任何标准输出都会邮寄到拥有该作业的用户的 unix 邮件帐户。虽然不确定 OSX。不幸的是,您必须等待作业完成才能发送邮件,然后才能查看输出。