首页 > 解决方案 > 从 /etc/rc.local 运行的脚本问题

问题描述

我正在尝试在引导时从 /etc/rc.local 在无头 Raspberry Pi 4(Raspbian buster lite - 基于 Debian)上运行 bash 脚本。我在 Pi 3 上成功地做了类似的事情,所以我很困惑为什么 Pi 4 会行为不端 - 或表现不同。从 /etc/rc.local 执行的脚本会触发,但似乎只是以看似随机的间隔退出,而没有说明它被终止的原因。

为了测试它,我简化了脚本并将以下内容粘贴到名为 /home/pi/test.sh 的测试脚本中:

#!/bin/bash
exec 2> /tmp/output      # send stderr from rc.local to a log file
exec 1>&2                # send stdout to the same log file
set -x                   # tell bash to display commands before execution

while true
do
        echo 'Still alive'
        sleep .1
done

然后我在退出行之前从 /etc/rc.local 调用它:

#!/bin/sh -e
#
# rc.local - executed at the end of each multiuser runlevel
#
# Make sure that the script will "exit 0" on success or any other
# value on error.

/home/pi/test.sh
echo $? >/tmp/exiterr #output exit code to /tmp/exiterr

exit 0

/tmp/output 的内容:

+ true
+ echo 'Still alive'
Still alive
+ sleep .1
+ true
+ echo 'Still alive'
Still alive
+ sleep .1

和 /tmp/exitrr 显示

0

如果我减少睡眠时间,/tmp/output 会更长(超过 6000 行没有睡眠)。任何想法为什么脚本在启动后不久就退出?

标签: bashdebianraspbian

解决方案


推荐阅读