如何从已经ALREADY运行的进程捕获stdout

编程入门 行业动态 更新时间:2024-10-28 04:21:17
本文介绍了如何从已经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/mivok/squirrelpouch/wiki/dtrace

注意:

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

You must disable dtrace restriction on El Capitan or later.

csrutil enable --without dtrace

更多推荐

如何从已经ALREADY运行的进程捕获stdout

本文发布于:2023-07-06 20:37:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1054529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   stdout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!