Running scripts at boot is a powerful way to automate backups, start a local server, or keep a monitoring daemon alive. The method described here works on any Android device that can install apps from the Play Store or F‑Droid and does not require root access. However, a few prerequisites will make the process smoother:
Make sure you have a recent backup of any important data. While the steps are safe, mis‑configured scripts can fill storage or consume battery if they loop indefinitely.
Termux provides a minimal Linux environment inside Android. For the most up‑to‑date version, use the F‑Droid repository rather than the Play Store, which often lags behind.
https://f-droid.org.During the initial launch Termux creates a private directory at /data/data/com.termux/files/home. This is where you will keep your scripts.
Running the latest packages reduces compatibility problems later on.
apt update && apt upgrade -y
The command updates the package index and upgrades all installed packages without prompting.
Optionally install a few utilities you will likely need in scripts:
apt install -y bash coreutils rsync curl wget git
Termux:Boot is a small companion app that runs a user‑defined script each time Android finishes booting.
If your device runs a manufacturer‑specific battery‑saving mode (e.g., Samsung’s Battery optimization), add Termux and Termux:Boot to the Never optimized list. Otherwise Android may kill the process before it can start your script.
Termux:Boot looks for executable files inside the hidden ~/.termux/boot directory. Any file placed there will be launched automatically after the system finishes booting.
mkdir -p ~/.termux/boot
hello.sh:
cat > ~/.termux/boot/hello.sh <<'EOF'
#!/data/data/com.termux/files/usr/bin/bash
# Simple test script – writes a timestamp to a log file
logfile="$HOME/boot.log"
echo "Boot script executed at $(date)" >> "$logfile"
EOF
chmod +x ~/.termux/boot/hello.sh
ls -l ~/.termux/boot/hello.sh
You should see something like -rwxr-xr-x.
The shebang line points to the Bash binary inside Termux’s private filesystem. Using the absolute path is important because the environment that runs at boot does not inherit the normal $PATH variable.
Before you trust a boot script, run it manually to confirm it works.
~/.termux/boot/hello.sh
Then check the log file:
cat ~/boot.log
You should see a line with the current date and time. If you get “permission denied”, double‑check that the script is executable and that the shebang points to the correct Bash location.
Now let Android do its job.
reboot (you may need to grant the Device admin permission the first time):
termux-reboot
cat ~/boot.log
If the timestamp reflects the time of the reboot, Termux:Boot successfully launched your script.
Now that the mechanism works, you can replace the test script with something useful. Below is a practical example that copies the Pictures directory to a folder on external storage every time the device boots. This is handy for devices that are rarely turned off – the backup will run the next time the user restarts after a crash or a software update.
cat > ~/.termux/boot/backup_pictures.sh <<'EOF'
#!/data/data/com.termux/files/usr/bin/bash
# Backup Pictures to external storage (SD card or USB‑OTG)
# Adjust SOURCE and DEST as needed
SOURCE="$HOME/storage/shared/Pictures"
DEST="/storage/XXXX-XXXX/AndroidBackup/Pictures"
# Ensure the destination directory exists
mkdir -p "$DEST"
# Use rsync to copy only new/changed files
rsync -av --delete "$SOURCE/" "$DEST/" >> "$HOME/boot.log" 2>&1
EOF
chmod +x ~/.termux/boot/backup_pictures.sh
Replace XXXX-XXXX with the actual UUID of your external storage. You can find it by running ls -l /storage in Termux.
The script appends its output to ~/boot.log, so you can later inspect whether the backup succeeded.
chmod +x) and that the shebang points to the correct Bash path. Adding set -x at the top of the script can help by printing each command to the log.~/.profile or ~/.bashrc. Use absolute paths for all files and binaries, or source the profile manually:
source "$HOME/.profile"
while ! ping -c1 -W1 8.8.8.8 >/dev/null 2>&1; do sleep 2; done
nohup with a timeout or schedule the task with cron (Termux’s cronie package) instead of launching it at every boot.termux-widget package, then create a shortcut script in ~/.shortcuts. This gives you a home‑screen widget to run the same script on demand.cronie and add entries to crontab -e. For example, to run a backup every day at 02:00:
0 2 * * * /data/data/com.termux/files/home/.termux/boot/backup_pictures.sh
#!/data/data/com.termux/files/usr/bin/python) is installed via pkg install python or pkg install nodejs.logcat can capture script output:
exec 2>&1
exec > >(logcat -t TERMUX_BOOT -s)
This way you can view logs with logcat -s TERMUX_BOOT without cluttering your home directory.Because the boot script runs with the same privileges as the regular Termux user, it can read, write, and delete any files in your private storage. Treat it like any other powerful automation tool:
termux‑storage‑request to request access to shared folders, and store secrets in the Android Keystore or a GPG‑encrypted file.Following these guidelines ensures your automation remains reliable without compromising battery life or data integrity.
Termux combined with the Termux:Boot add‑on gives Android users a fully fledged, root‑free way to launch Linux‑style scripts at system start. By creating an executable file in ~/.termux/boot, granting the necessary background permissions, and handling the quirks of the boot environment (PATH, network availability, battery optimisation), you can automate backups, start servers, or kick off any custom routine you would normally run on a desktop Linux box.
Feel free to adapt the examples, experiment with other interpreters, and integrate the boot script into a larger automation strategy using cronie or Termux:Widget. With careful testing and the safety tips above, your Android device can become a miniature, always‑on Linux workstation without ever needing to be rooted.









