【问题标题】:Can rc.local wait for Bash script to finish before bootingrc.local 可以在启动前等待 Bash 脚本完成吗
【发布时间】:2017-07-03 11:50:40
【问题描述】:

我正在运行 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:

Sample Boot Output 同时显示所有内容,并且不允许用户输入新的主机名。一旦显示这些行,系统就会继续进入登录屏幕。系统的期望行为将允许用户有时间输入新的主机名,然后显示之前提交的输入。

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

【问题讨论】:

标签: linux bash boot


【解决方案1】:

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

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

【讨论】:

  • 感谢您对启动过程中发生的情况的解释。它有助于更​​多地了解系统。解决方法:exec
猜你喜欢
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2016-01-22
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多