首页 > 解决方案 > 如何在 init=/bin/bash 的早期引导阶段将标准输出重定向到文件

问题描述

我有一个在早期启动 Bash 环境中运行的脚本,其中init=/bin/bash. 我想将脚本输出重定向到日志文件。但是,我收到一条错误消息,指出/dev/fd/XX不可用。

有没有办法做到这一点?

这是我的内核配置文件(在 Raspberry Pi 上),cmdline.txt(为提高可读性而添加了新行;这实际上是一长行)

console=serial0,115200 console=tty1 root=PARTUUID=067e19d7-02 rootfstype=ext4
elevator=deadline fsck.repair=yes rootwait 
init=/bin/bash -c 
  "mount -t proc proc /proc; 
   mount -t sysfs sys /sys; 
   mount /boot; 
   source /boot/unattended"

代替/boot/unattended

mount -t tmpfs tmp /run
mkdir -p /run/systemd
mount / -o remount,rw

# This is where I want to start redirecting command output
ls -la 
# Would like the above command output to go to a log file

通常,我会添加如下所示的代码片段……但这是在早期引导 shell 中失败的代码片段。

# Redirect stdout and stderr
exec >  >(tee -ia /boot/log/script_stdout.log >&1)
exec 2> >(tee -ia /boot/log/script_stderr.log >&2)

在早期的 Bash shell 中,这会导致:

bash: /dev/fd/62: No such file or directory

不用说,只有一些tty文件,urandom,几个(我认为是)RPi 视频设备节点(vc...watchdog{0}zero.

标签: bashraspberry-pistdoutinitstderr

解决方案


推荐阅读