gpt4 book ai didi

linux - rc.local 可以在启动前等待 Bash 脚本完成吗

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:53 26 4
gpt4 key购买 nike

我正在运行 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 会提示用户输入新的主机名,然后将主机名返回给用户。

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

示例引导输出:

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

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

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

最佳答案

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

您将需要手动重定向读取以始终使用固定终端(例如 read </dev/tty0 )或打开虚拟控制台以进行用户输入(例如 openvt -s -w /root/hostnameBoot )。参见 this answer了解更多详情。

关于linux - rc.local 可以在启动前等待 Bash 脚本完成吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42238869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com