>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Blockchain Embedded Security
Java

How to Turn an Old Android Phone into a Standalone Server Using Termux Boot

Many of us have an old Android smartphone gathering dust at home. The screen is slightly cracked, the battery doesn't hold a charge well, and you can't sell it for a reasonable price. However, inside there's a perfectly functional quad-core processor and a couple of gigabytes of RAM. This hardware makes for an excellent home server for Telegram bots, lightweight automation scripts, or a local SSH node.

The Termux terminal has long turned Android into a full-fledged Linux environment. But there's an annoying caveat. If the smartphone shuts down due to a dead battery or reboots after an update, all running processes will die. You'll have to manually open the app, type in commands, and restart services from scratch.

The Termux:Boot addon helps solve this problem. It's a utility that hooks into the operating system's boot event and runs user scripts.

How Autostart Works in Android

Android is strict about background processes. The system doesn't allow apps to start alongside the OS without the user's direct consent and special permissions.

Termux:Boot works as a bridge between the system event BOOT_COMPLETED and your Linux environment inside Termux. When the smartphone powers on, the addon receives a signal from the system, looks into the user's designated directory, and runs the executable files found there in order.

There's a nuance with app signing here. Termux and Termux:Boot communicate through shared access rights. Due to Android's security rules, both apps must be signed with the same developer key.

If you installed the main Termux from F-Droid, then the Termux:Boot plugin must also come strictly from F-Droid. Attempting to mix a Google Play build with an F-Droid APK will result in an access error. If you want to test the latest commits, developers upload debug builds to GitHub Actions. But remember: changing the installation source will require completely removing Termux and all its plugins.

Step-by-Step Setup

The setup scheme is simple. It all comes down to creating one folder and a couple of text files.

First, you need to install the Termux:Boot addon itself. After installation, be sure to find its icon in the app menu and tap it once. This grants the addon permission to intercept system autostart. Without this action, Android will simply block background startup.

Then open the Termux terminal and create the directory for scripts:

mkdir -p ~/.termux/boot/

All files placed in this folder will become autostart scenarios. If there are multiple files in the folder, the plugin will execute them in alphabetical order.

To prevent the smartphone from falling into deep sleep five minutes after startup, the command termux-wake-lock comes in handy at the very beginning of scripts. It forces Android to keep the CPU in an awake state.

Working Script Examples

Let's walk through a couple of practical scenarios. A common task is to get SSH access to the device immediately after the system starts.

Let's create the file ~/.termux/boot/start-sshd:

#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
sshd

Make sure to make the file executable, otherwise the system will ignore it:

chmod +x ~/.termux/boot/start-sshd

Now when you reboot the phone, Termux will bring up the SSH server in the background. You'll be able to connect to it over the network without any screen interaction.

If you use the termux-services package for managing system daemons (a systemd equivalent in regular Linux distributions), the startup script looks like this:

#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
source /data/data/com.termux/files/usr/etc/profile.d/start-services.sh

Create this file in ~/.termux/boot/start-services, give it execute permissions, and all services enabled via the sv-enable command will start automatically.

Practical Use Cases

The main value of the project lies in complete autonomy. The smartphone turns into a quiet mini-server with its own built-in uninterruptible power supply.

Here are a few clear scenarios:

  • Telegram bot hosting. The bot starts up with the device and runs without constant supervision.
  • File sync via Syncthing. The phone works as a 24/7 node for backing up photos or documents.
  • Home network monitoring. A script periodically checks router or external service availability and sends notifications on failures.
  • Local database server like SQLite or PostgreSQL for collecting logs from smart home devices.

Debugging and Pitfalls

There are no perfect solutions, and Termux:Boot has its own quirks.

Aggressive power saving in modern Android firmwares loves to kill background processes. MIUI, One UI, and EMUI are especially guilty of this. To make the plugin work without issues, go to Android's battery settings and disable battery optimization for Termux and Termux:Boot. On MIUI-like firmwares, you should also manually allow the addon to autostart in the app settings.

Then it's worth checking the logging functionality. If your script isn't starting, redirect error output to a file right in the script:

#!/data/data/com.termux/files/usr/bin/sh
exec 2>&1
exec 1>/sdcard/termux-boot.log
termux-wake-lock
echo "Boot completed at $(date)"

This way you'll immediately see if the script stumbled on environment variable paths or couldn't find the required binary.

After updating apps or flashing the device, you sometimes need to manually launch Termux:Boot again via the desktop icon.

Conclusion

Termux:Boot is an extremely simple plugin with a single task, and it does that job well. The Java source code takes up minimal space, and the logic is understandable to anyone familiar with basic bash.

If you're planning to build a home server or compact automation system from an old smartphone, this addon will save you time during accidental device reboots.

Related projects