Run Linux Scripts Automatically at Android Boot Using Termux

13 min read Step‑by‑step guide to set up Termux and Termux:Boot so your Linux scripts run automatically every time your Android device starts, without rooting. October 01, 2026 09:30 How to Run Linux Scripts Automatically at Android Boot Using Termux and Termux:Boot (No Root)

Before you start

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:

  • Android 7.0 (API 24) or newer – older versions lack the background‑execution permissions needed by Termux:Boot.
  • At least 200 MB of free internal storage for Termux packages and your scripts.
  • Battery level above 30 % (or keep the device plugged in) for the first reboot after setup.
  • Wi‑Fi or mobile data if your script needs network access during boot.

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.

1. Install Termux

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.

  1. Open your browser and navigate to https://f-droid.org.
  2. Download the F‑Droid client and install it (you may need to enable Install unknown apps for the browser).
  3. Inside F‑Droid, search for Termux and tap Install.
  4. Once installed, launch Termux. It will display a welcome message and start a first‑time setup.

During the initial launch Termux creates a private directory at /data/data/com.termux/files/home. This is where you will keep your scripts.

2. Update the Termux environment

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

3. Install the Termux:Boot add‑on

Termux:Boot is a small companion app that runs a user‑defined script each time Android finishes booting.

  1. Return to F‑Droid and search for Termux:Boot.
  2. Tap Install. The app does not have a UI; it lives in the background.
  3. After installation, open Android Settings → Apps → Termux:Boot → Permissions and enable Run in background (or “Allow background activity”). This permission is required on Android 9+.

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.

4. Create your first boot 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.

  1. In Termux, create the directory if it does not exist:
    mkdir -p ~/.termux/boot
  2. Create a simple test script called 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
  3. Make the script executable:
    chmod +x ~/.termux/boot/hello.sh
  4. Verify the file permissions:
    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.

5. Test the script without rebooting

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.

6. Reboot and verify automatic execution

Now let Android do its job.

  1. From Termux, type reboot (you may need to grant the Device admin permission the first time):
    termux-reboot
  2. Wait for the device to power back on. After the home screen appears, open Termux.
  3. Inspect the log file again:
    cat ~/boot.log

If the timestamp reflects the time of the reboot, Termux:Boot successfully launched your script.

7. Example: Automatic daily backup of a folder

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.

8. Common pitfalls and troubleshooting

  • Script never runs. Check that Termux:Boot has the Run in background permission and is excluded from battery optimisation. On Android 12+, you may also need to enable Allow background activity in Settings → Apps → Termux:Boot → Battery.
  • Log file is empty. Verify that the script is executable (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.
  • Path‑related errors. The boot environment does not source ~/.profile or ~/.bashrc. Use absolute paths for all files and binaries, or source the profile manually:
    source "$HOME/.profile"
  • Network not available. Android may still be establishing Wi‑Fi when the script runs. If your script needs internet, add a short wait loop:
    while ! ping -c1 -W1 8.8.8.8 >/dev/null 2>&1; do sleep 2; done
  • Battery drain. A script that runs a long‑lasting service (e.g., a web server) will keep the CPU awake. Use nohup with a timeout or schedule the task with cron (Termux’s cronie package) instead of launching it at every boot.

9. Advanced tips

  • Use Termux:Widget for manual triggers. Install the termux-widget package, then create a shortcut script in ~/.shortcuts. This gives you a home‑screen widget to run the same script on demand.
  • Schedule recurring jobs. Instead of relying on boot, install 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
  • Run Python or Node scripts. The same boot directory works for any executable. Ensure the interpreter (e.g., #!/data/data/com.termux/files/usr/bin/python) is installed via pkg install python or pkg install nodejs.
  • Redirect output to syslog. Android’s 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.

10. Safety and performance considerations

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:

  • Test scripts thoroughly. Run them manually several times before relying on the boot trigger.
  • Guard against infinite loops. Add a maximum runtime or a watchdog that aborts after a certain duration.
  • Keep storage usage in check. If a script creates log files, rotate them or delete old entries to avoid filling up the device.
  • Respect user privacy. Do not embed hard‑coded passwords in scripts. Use Android’s 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.

Wrap‑up

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.

User Comments (0)

Add Comment
We'll never share your email with anyone else.