Skip to content

Automation

Ready-made recipes combining the 7 tools with cron, tmux, jq, and SSH. All use --json or exit codes to be easy to orchestrate.

Prerequisites

pkg install -y cronie jq openssh tmux
# enable cron in Termux
sv-enable crond   # or: crond

In Magisk → Superuser → Termux → config, set request timeout: 10 minutes or forever, so cron doesn't hang waiting for permission.


1. Low battery alert

Fires an alert (audio + notification) when battery drops below 20%.

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/battery-alert.sh
set -u

THRESHOLD=20
READ="$(battery --json)"
LEVEL="$(printf '%s' "$READ" | jq -r '.level')"
STATUS="$(printf '%s' "$READ" | jq -r '.status')"

if [ "$LEVEL" -lt "$THRESHOLD" ] && [ "$STATUS" = "Discharging" ]; then
    termux-notification --title "Low battery" \
        --content "${LEVEL}% — plug in the charger" \
        --priority high
    termux-tts-speak "Battery at ${LEVEL} percent" 2>/dev/null || true
fi
# every 15 min
*/15 * * * * /data/data/com.termux/files/usr/bin/bash ~/.local/bin/battery-alert.sh

Requires termux-api installed and the Termux:API app from Play Store/F-Droid.


2. Screen off at night, on in the morning

Saves pixels and battery on a 24/7 server device.

# midnight: off
0 0 * * * screen-toggle --off -q
# 6am: on
0 6 * * * screen-toggle --on -q

If you only want to turn off if currently on (not turn on if already off), --off is already idempotent — it reads state before acting.


3. Apply "server" profile via SSH in bulk

If you have multiple devices, automate profile application:

#!/data/data/com.termux/files/usr/bin/bash
# apply-profile.sh <host> <profile>
HOST="$1"
PROFILE="${2:-servidor-seguro}"

ssh "$HOST" "appctl --profile $PROFILE -n && appctl --profile $PROFILE"

Use:

for ip in 192.168.1.{10..15}; do
    ssh root@$ip "appctl --profile servidor-seguro --yes"
done

appctl --yes (if available in your version) skips interactive confirmation. Without it, pipe yes | or use --non-interactive.


4. Monitor RAM and swap (NDJSON export)

Collects to a file that can be ingested by a dashboard (Grafana, Datadog, etc.):

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/mem-export.sh
while true; do
    battery --json
    # build a combined object with swap
    createswap --status --json 2>/dev/null | jq -c --arg ts "$(date -Iseconds)" \
        '. + {timestamp: $ts, source: "headless-lab"}'
    sleep 60
done >> ~/lab-metrics.ndjson

NDJSON format (one JSON line per reading) is trivial to consume:

# last 10 battery level readings
tail -n 10 ~/lab-metrics.ndjson | jq -c '{ts: .timestamp, level: .level}'

5. Detect RAM-hogging app

Combines user-list --json + jq to detect spikes:

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/ram-watch.sh
THRESHOLD_MB=300

user-list -3 --sort mem --json | jq --argjson thr $((THRESHOLD_MB * 1024)) -r '
    .users[]
    | select(.rss_kb > $thr)
    | "[\(.rss_kb/1024|floor) MB] \(.user) → \(.pkgs[0]) (\(.top_proc))"
' | while IFS= read -r line; do
    termux-notification --title "High RAM" --content "$line" --priority normal
done

Cron:

*/10 * * * * /data/data/com.termux/files/usr/bin/bash ~/.local/bin/ram-watch.sh

6. Screen + touch in "remote display" mode

Scenario: the device is on a shelf, showing a dashboard but with no physical interaction (touch disabled so nobody messes with it). Screen on from morning to night, touch always off, with auto-recovery.

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/kiosk-mode.sh
set -u

# ensure touch is off (recover from reboot)
touch-ctl status | grep -q ATIVO && touch-ctl off

# turn screen on if off
screen-state -q || screen-toggle --on -q

# schedule off at 22h, on at 8h
( crontab -l 2>/dev/null; echo "0 22 * * * screen-toggle --off -q" ) | crontab -
( crontab -l 2>/dev/null; echo "0 8 * * * screen-toggle --on -q" )  | crontab -

Run once on boot:

# add to ~/.bashrc (runs when Termux opens)
~/.local/bin/kiosk-mode.sh

Or schedule via Magisk service.d:

su -c 'cat > /data/adb/service.d/kiosk.sh <<EOF
#!/system/bin/sh
sleep 30  # wait for boot
su -c "touch-ctl off"
su -c "screen-toggle --on -q"
EOF
chmod +x /data/adb/service.d/kiosk.sh'

7. Automatic appctl state backup

Before a big change, snapshot; after, verify.

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/appctl-snap.sh
SNAP_DIR=~/.appctl/manual-snapshots
mkdir -p "$SNAP_DIR"
TS="$(date +%Y%m%d-%H%M%S)"
appctl --list > "$SNAP_DIR/list-$TS.txt"
user-list --json > "$SNAP_DIR/users-$TS.json"
echo "snapshot at $SNAP_DIR/$TS"

Weekly cron:

0 3 * * 0 /data/data/com.termux/files/usr/bin/bash ~/.local/bin/appctl-snap.sh

8. Graceful shutdown on low battery

#!/data/data/com.termux/files/usr/bin/bash
# ~/.local/bin/graceful-shutdown.sh
set -u

LEVEL=$(battery --json | jq -r '.level')

if [ "$LEVEL" -lt 5 ]; then
    # tear down ZRAM and swapfile without loss (if RAM can take it)
    createswap --clear all 2>/dev/null || true

    # final notification
    termux-notification --title "Shutting down" --content "Battery at ${LEVEL}%"

    # shut down (requires extra permission)
    su -c 'setprop sys.powerctl shutdown'
fi

Caution

setprop sys.powerctl shutdown powers off the device immediately. Use only in a server scenario where you have physical/ADB access to power it back on.


systemd-like template with Magisk

For a service that runs in the background even with the screen off, create a script in /data/adb/service.d/:

su -c 'cat > /data/adb/service.d/lab-monitor.sh <<EOF
#!/system/bin/sh
# runs in background, outside Termux
while true; do
    /data/data/com.termux/files/usr/bin/battery --json >> /data/local/tmp/lab.log
    sleep 60
done
EOF
chmod +x /data/adb/service.d/lab-monitor.sh'

Magisk runs everything in service.d/ after boot. The script survives closing Termux — it only stops if the device reboots (and restarts on the next boot).


See also