This article introduces a technique for using a custom startup script during system boot. This method is particularly useful for systems with a read-only filesystem, providing a convenient way to temporarily modify system configurations or launch additional programs without altering the main filesystem.
Implementation
The standard startup script in many embedded Linux systems is /etc/init.d/rcS
. While applications can be launched directly from here, it is better practice to call a separate application-specific script. This approach simplifies future modifications, as you only need to update the application script, not the core system script.
First, add a call to your application's main startup script, which we'll call init_app.sh
, within /etc/init.d/rcS
. The purpose of init_app.sh
is to launch your primary application and initialize its configuration parameters.
Next, to create a hook for a custom script, add the following logic inside init_app.sh
. This code checks for a script named run.sh
located in a read-write partition (e.g.,/mnt/data/
) and executes it if found.
if [ -f /mnt/data/run.sh ]
then
/bin/sh /mnt/data/run.sh &
fi
There are two key points about this implementation:
- The script is launched using
/bin/sh
. This ensures it will run regardless of whether it has execute permissions. - The ampersand (
&
) runs the script in the background, which prevents it from blocking the execution of the maininit_app.sh
script.
Common Use Cases
With this hook in place, you can easily perform temporary modifications in the field. For example, to launch an extra program, you can simply add the command to run.sh
without altering the core application startup process.
This method is also frequently used for applying system configuration changes. Commands for tasks such as modifying the network routing table, creating symbolic links, or clearing log files can be placed in therun.sh
script as needed.