Search code examples
linuxbashboot

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


I am running the rolling release of Kali Linux, and have started to write a script that is executed by rc.local upon booting, that will be allow the user update the hostname of the computer.

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

hostnameBoot Script:

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

# Solution Added
exec < /dev/tty0

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

#exit 0

As you can see, currently hostnameBoot prompts the user to enter a new hostname, and then returns the hostname to the user.

Upon booting, rc.local execute the script, but does not prompt the user to enter a new hostname.

Sample Boot Output:

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

The Sample Boot Output shows all at once and does not allow the user to enter a new hostname. Once the lines are shown, the system then continues to the login screen. Desired behavior of the system would allow the user time to enter a new hostname, then be presented with the input previously submitted.

note: The script is not the end product, it was just a proof of concept using rc.local to trigger the script.


Solution

  • Boot scripts, including rc.local, are usually not executed in interactive mode (i.e. with a fully functioning terminal where the user can enter data). Their output is redirected to the console (so you can see the boot messages) but the input is most likely /dev/null (so read returns immediately with nothing to read).

    You will need to either manually redirect the read to use a fixed terminal all the time (e.g. read </dev/tty0) or open a virtual console to do the user input in (e.g. openvt -s -w /root/hostnameBoot). See this answer for more details.