The goal: shell scripts that talk to Android
Termux on its own is a Linux userland that knows very little about the phone it runs on. Termux:API bridges that gap, exposing Android features such as notifications, the clipboard and the battery as ordinary commands. Combine them with Termux:Widget and Termux:Boot and Termux API automation becomes genuinely useful: one-tap scripts on your home screen and jobs that start when the phone boots.
This is part four of my Termux series, after SSH in Termux.
Step 1: install the app and the package
You need two pieces: the Termux:API Android app, which holds the permissions, and the termux-api package, which provides the command-line tools.
Install Termux from F-Droid or the project’s GitHub releases. The Google Play listing is still labelled an experimental branch with missing functionality, and Termux plus its plugin apps must all come from the same source because they share a signing key. Mixing sources gives you INSTALL_FAILED_SHARED_USER_INCOMPATIBLE errors.
pkg update
pkg install termux-api
Test it with something harmless:
termux-battery-status
You should get JSON back. If the command hangs, the Termux:API app is missing or came from a different source to Termux itself.
Step 2: the useful commands
termux-notificationposts a notification, with options for title, content and an ID so you can update it later.termux-toastshows a quick on-screen message.termux-clipboard-getandtermux-clipboard-setread and write the clipboard.termux-battery-statusreturns charge level, temperature and charging state as JSON.termux-sharehands a file to Android’s share sheet.termux-vibrateandtermux-tts-speakgive you physical and spoken feedback.
Every command supports -h for its options; check there rather than guessing, as features vary between versions. Some, such as location or SMS, prompt for Android permissions the first time.
Parsing the JSON
Many commands return JSON, so install jq and pull out what you need:
pkg install jq
termux-battery-status | jq -r .percentage
Step 3: a few practical scripts
A low-battery warning you can run from cron or a loop:
#!/data/data/com.termux/files/usr/bin/bash
pct=$(termux-battery-status | jq -r .percentage)
if [ "$pct" -lt 20 ]; then
termux-notification --title "Battery low" --content "${pct}% left"
fi
Paste whatever is on the clipboard into a notes file with a timestamp:
#!/data/data/com.termux/files/usr/bin/bash
echo "$(date '+%F %T') $(termux-clipboard-get)" >> ~/notes.txt
termux-toast "Saved"
Notify yourself when a long build finishes, which pairs nicely with compiling llama.cpp:
make && termux-notification --title "Build done" || termux-notification --title "Build failed"
Step 4: one-tap scripts with Termux:Widget
Install the Termux:Widget app from the same source as Termux. It lists scripts stored in ~/.shortcuts and puts them on your home screen:
mkdir -p ~/.shortcuts
cp clip-to-notes.sh ~/.shortcuts/
chmod +x ~/.shortcuts/clip-to-notes.sh
Add the widget from your launcher and tap a script to run it. Scripts placed in ~/.shortcuts/tasks run in the background without opening a terminal window, which suits quick jobs such as the clipboard example.
Step 5: run at startup with Termux:Boot
Install Termux:Boot, open it once so Android registers it, then put executable scripts in ~/.termux/boot/. A common example is starting sshd on boot:
mkdir -p ~/.termux/boot
cat > ~/.termux/boot/start-sshd <<'EOF'
#!/data/data/com.termux/files/usr/bin/sh
termux-wake-lock
sshd
EOF
chmod +x ~/.termux/boot/start-sshd
Taking a wake lock first helps stop Android from putting Termux to sleep straight away.
Troubleshooting and limitations
- Commands hang forever: the Termux:API app is not installed, or its signature does not match Termux. Reinstall everything from one source.
- Scripts die in the background: battery optimisation is killing Termux. Exempt Termux in Android’s battery settings, and use a wake lock for long jobs.
- Permission errors: grant the relevant permission to the Termux:API app in Android settings.
- Boot scripts do nothing: open Termux:Boot once after installing it, and check the scripts are executable.
Wrap-up
Termux API automation turns the phone from a terminal you visit into a device your scripts can act on. Start with notifications and the clipboard, add Termux:Widget for one-tap actions, and use Termux:Boot for anything that should simply always be running.

Leave a Reply
You must be logged in to post a comment.