可以 rc.local 等待 Bash 脚本完成后再启动吗

Can rc.local wait for Bash script to finish before booting

我是 运行 Kali Linux 的滚动版本,并且已经开始编写一个脚本,在启动时由 rc.local 执行,这将允许用户更新计算机的主机名。

rc.local:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/root/hostnameBoot

exit 0

主机名引导脚本:

#!/bin/bash
# /etc/init.d/hostnameBoot

# Solution Added
exec < /dev/tty0

echo "Enter desired hostname:"
read hostname
echo "New hostname: $hostname"

#exit 0

如您所见,目前 hostnameBoot 提示用户输入新的主机名,然后 returns 将主机名提供给用户。

启动时,rc.local 执行脚本,但不提示用户输入新的主机名。

示例引导输出:

- misc boot info - 
Enter desired hostname:
New hostname:

示例引导输出一次显示所有内容,并且不允许用户输入新的主机名。显示线条后,系统将继续到登录屏幕。系统的预期行为将允许用户有时间输入新的主机名,然后显示先前提交的输入。

注意:脚本不是最终产品,它只是使用 rc.local 触发脚本的概念证明。

引导脚本,包括 rc.local,通常不会以交互模式执行(即使用功能齐全的终端,用户可以在其中输入数据)。它们的输出被重定向到控制台(因此您可以看到启动消息)但输入很可能是 /dev/null(因此 read returns 立即没有任何内容可读)。

您将需要手动重定向读取以始终使用固定终端(例如 read </dev/tty0)或打开虚拟控制台以进行用户输入(例如 openvt -s -w /root/hostnameBoot)。有关详细信息,请参阅 this answer