How to use Termux to put your old Android to use

What is Termux?

Termux is a Linux terminal emulator you can install on your Android.

Termux running on my Android phone

From this terminal, you can type in commands just like in Linux, and it has several packages available to install using the same repo methods you use on Linux (pkg install <name_of_package>). Essentially, once you install Termux, you have a Linux environment running on your Android giving you the ability to build or run anything like on any other Linux environment. My favorite ideas for this kind of setup is to build and run any bots to automate some of the tasks I find myself frequently doing.

How can I install Termux?

Historically, you could install Termux from the Play Store, and while it is still available there, it does not have the updates that point to the latest package repositories. I found this out the hard way when I installed it on an old Android phone direct from the Play store. I was trying to install python using the package manager, and I kept getting an error saying that the repository couldn’t be found. While you can update your config files to point to the correct repositories, I just found that FDroid has Termux available for install with all the latest updates. (The owners of Termux decided to move all their support to FDroid).

What is FDroid?

FDroid, just like the Google Play store is a package manager where you can download, install, and keep apps up-to-date on your Android. FDroid is much more popular with the open-source community, and FDroid has looser rules on the apps they host. The Google Play store has a lot more rules, partially because of contract deals with certain companies, and also to help protect the novice user. That’s not to say the open-source software and package managers like FDroid are inherently unsafe, but it is wise that you take the time to research whatever it is you decide to download and install and give permissions to on any of your devices.

How can I get FDroid

FDroid is not available on the Google App store. This is where you’ll have to do what is called “side loading” an app. All the Androids I’ve used have made this pretty easy. Android executables are in a file called an “apk,” and the file extension for these files is “apk” (i.e.: fileToInstall.apk). Use your Android browser to navigate to the FDroid Website.

FDroid Website

Tap the button to download. Once it’s saved to your downloads folder (most Androids should already have this available in your list of apps, but if not, you can install a simple file manager from the Google Play store which should allow you to easily find the downloads folder). Before installing, you’ll need to make sure your Android settings are updated to allow you to install sources outside of Google Play. Once you have your phone set up to install from sources outside of Google Play, you can tap the APK file in the Downloads folder, and follow the prompts to install (and to indicate you trust this source if you get asked). As a side note, while I trust this source, I encourage anyone doing this to do whatever research you deem necessary to determine whether or not to trust a software source.

SSH Server for Termux

Once you have Termux installed, if you’re like me, you’ll find that using the phone keyboard to type commands is painstakingly slow, and quickly gets frustrating without a regular keyboard. While you can get a blue tooth keyboard and mouse for your Android, I’ve found that a far more convenient way to use it is to install an SSH server and control my Android using a terminal on my computer. I used the OpenSSH package to set up an SSH server on Termux. This page has the instructions I followed to get it set up.

I used key encryption rather than the username and password in my setup so that I didn’t need to type in a password every time. I’ll leave it to you to determine which authentication method you prefer.

Once the server was set up, I was able to open a terminal and SSH into my phone and run commands just like using SSH into any other Linux system.

SSH Into Termux Example

What should I run on Termux anyway?

This is the hardest question to answer because it is really about creativity and what you feel like doing. In fact, this is what I spent the most time on before coming up with something to at least get started. The idea on this is also how much time you really want to end up spending on a single project.

For me, I really love the idea of having bots to help automate the things I do every day. I also didn’t want to have to spend a massive amount of time between having Termux set up on my old Android and getting something useful to run. I also didn’t want to just run something that I would later replace but rather try to come up with something that I could gradually build more off of in time.

After brainstorming and shopping all over GitHub, I ended up coming up with my initial idea: have a bot that records BTC prices every minute in real time. The idea behind this was to be able to save BTC price movements and use it to run technical analysis scenarios on it. This also gave me new ideas for the future: it can be updated to track more crypto currencies as well as any other news that may affect price movement. This really is a bot that gathers data that can be further analyzed to make predictions on which direction BTC (or other crypto currencies when I make those changes) may go based on current events. No simple program existed on GitHub that provides this functionality, so I wrote up a Python script that did what I needed:

from urllib.request import urlopen
import time

count = 0
maxCount = 10000000  # 10,000,000
fileCount = 0
maxFileCount = 10000  # 10,000

while True:
    url = 'https://api.coindesk.com/v1/bpi/currentprice/USD.json'
    response = urlopen(url)
    webContent = response.read()
    file = open("output" + str(fileCount) + ".txt", "a")
    file.write(str(webContent) + '\n\n')
    # print(webContent)
    file.close()
    count += 1
    if count == maxCount:
        fileCount += 1
        count = 0
    if fileCount == maxFileCount:
        break
    time.sleep(60)

In short, I call the API provided by CoinDesk to get the BTC current price in USD and write it to a local file. This bot is set up to query the API every minute. Also, to avoid filling out too much data, I have a hard limit to 10,000,000 entries in a single file before writing to a new file, then having a max of 10,000 files before the bot stops just so that the storage on my old Android does not get overrun.

This code is overly simple and doesn’t do a whole lot, but it was something that I was able to get up and running fairly quickly while also giving me a starting point that I can build more functionality on. Let me know what you guys come up with.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *