macOS:如何使用终端重定向启动后的进程的STDERR/STDOUT? [英] macOS: How to redirect STDERR / STDOUT of a process AFTER started, using Terminal?

查看:85
本文介绍了macOS:如何使用终端重定向启动后的进程的STDERR/STDOUT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在macOS中,通常可以通过以下方式直接在终端中执行二进制程序来执行进程,从而获得一些可观的控制台/外壳输出:

In macOS one can normally get some appreciable console / shell output by executing a process by executing it's binary directly in the Terminal via:

/Applications/SOME_APPLICATION.app/Contents/MacOS/SOME_APPLICATION

这对于不时调试和捕获发生的错误非常有用.随着Catalina(10.15)的引入,不鼓励以脚本等方式直接执行应用程序,并且会导致各种问题,最终需要使用/usr/bin/open .

This can be very useful from time to time for debugging and catching errors that occur. With the introduction of Catalina (10.15), direct execution of applications in this fashion is discouraged from scripts, etc. and causes various problems, ultimately requiring the use of /usr/bin/open.

在流程启动后,我们如何重定向其STDERR/STDOUT?

How can we redirect STDERR / STDOUT of a process after it's been started?

以前在Linux上已经对此主题进行了讨论,但是尚不清楚是否会立即为macOS工作.

There has been some discussion on this topic previously for Linux, but it's not clear as to whether some of this will work out for macOS now.

reptyr 如果可以重新用于macOS,那就太棒了,因为它已经主要用于FreeBSD.

reptyr would be fantastic if it could be repurposed for macOS since it already works mostly for FreeBSD.

推荐答案

This答案是从2013年开始的,但仍然很重要.

在山狮"之前,所有由发起的流程都已启动,包括常规应用程序,具有其stdout和stderr文件描述符转发到系统日志.在Mountain Lion及更高版本中,stdout和stderr无法启动已启动的托管应用程序.仅消息明确发送到系统日志的文件将在那里结束.

Prior to Mountain Lion, all processes managed by launchd, including regular applications, had their stdout and stderr file descriptors forwarded to the system log. In Mountain Lion and above, stdout and stderr go nowhere for launchd managed applications. Only messages explicitly sent to the system log will end up there.

如果您正在编写应用程序,并且希望显示一些输出在控制台中,然后采用基于syslog(3)或asl(3)构建的API反而.NSLog是一种这样的API,它具有将日志记录到stderr也是如此,因此无论您如何操作,都可以轻松查看输出启动您的应用程序.如果您想要该功能但想要直接使用asl或syslog,则需要查看ASL_OPT_STDERR选项用于asl_open,而LOG_PERROR选项用于分别是openlog.

If you're writing an application and would like some output to show up in the console, then adopt an API built on syslog(3) or asl(3) instead. NSLog is one such API, and it has the advantage of logging to stderr too so you can easily see your output no matter how you've launched your application. If you'd like that functionality but want to use asl or syslog directly then you'll want to look in to the ASL_OPT_STDERR option to asl_open, and the LOG_PERROR option to openlog respectively.

基本上,当您双击一个应用程序(与/usr/bin/open 相同)时,没有stdout/stderr.开发人员应将所有相关的输出/错误发送到可用的日志记录API,例如NSLog.

Basically, when you double-click an app (same as /usr/bin/open) there is no stdout/stderr. The dev is expected to send any relevant output/errors to the available logging APIs such as NSLog.

这篇关于macOS:如何使用终端重定向启动后的进程的STDERR/STDOUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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