5

全志D1H芯片 Tina 如何查看通过 procd init 脚本启动的应用输出到 stdout/stderr 的打...

 1 year ago
source link: https://blog.51cto.com/u_15380233/5718421
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

全志D1H芯片 Tina 如何查看通过 procd init 脚本启动的应用输出到 stdout/stderr 的打印信息?

精选 原创

神棍地海棠 2022-09-28 10:00:16 ©著作权

文章标签 应用程序 重定向 守护进程 文章分类 嵌入式Linux 嵌入式 阅读数153

问题描述

当我们使用 procd init 脚本让某个应用程序实现开机自启时,会发现应用程序中原本通过 printf/fprintf 等输出到 stdout/stderr 的打印信息都无法从串口或 adb shell 中看到了。

这些打印默认是输出到什么地方?我们可以如何看到这些打印?

原因

一般情况下,当用户在终端中执行命令来运行某个应用程序时,stdin/stdout/stderr 就确定下来是在当前终端,因此应用程序的打印信息自然能从当前终端中显示出来。

而如果该应用程序是通过 procd init 脚本进行开机自启,它会被认为是一个守护进程(daemon)。守护进程是随系统自启的,它们有可能在用户登录终端之前就已经开始运行了,也无法得知用户是从哪个终端登录,因此也就无法将打印信息输出到用户所在的终端。

解决方法

一般来说,要获取守护进程的打印,需要通过 syslog 之类记录系统整体日志的方法。procd init 脚本也提供了方法将应用程序的打印重定向到 syslog 中。

下面是一个简单的 procd init 脚本例子,它会启动应用程序 /usr/bin/foobar,但我们默认没法看到 foobar 输出到 stdout/stderr 的打印:

#!/bin/sh /etc/rc.common

START=50

USE_PROCD=1

start_service() {
    procd_open_instance
    procd_set_param command /usr/bin/foobar
    procd_close_instance
}

通过增加“procd_set_param stdout 1”和“procd_set_param stderr 1”两个参数,可将其输出到 stdout/stderr 的内容重定向到 syslog:

#!/bin/sh /etc/rc.common

START=50

USE_PROCD=1

start_service() {
    procd_open_instance
    procd_set_param command /usr/bin/foobar
    procd_set_param stdout 1    # 将其 stdout 的内容重定向到 syslog
    procd_set_param stderr 1    # 将其 stderr 的内容重定向到 syslog
    procd_close_instance
}

如此设置后,就可以从 syslog 中看到 foobar 应用程序输出的打印。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK