When an app misbehaves, a device freezes, or you simply want to keep a history of what Android is doing, the logcat command is the most direct window into the system. However, by default Android only shows logs while you have a terminal open, and the buffer is cleared on reboot. This tutorial shows you how to turn your Android phone into a self‑contained log‑collector that runs in the background, survives reboots, and stores logs safely on internal storage – all without rooting.
System logs contain timestamps, process IDs, and stack traces that are invaluable for diagnosing:
Because Android clears the log buffer on reboot and the built‑in Developer Options → Show logcat view disappears when the screen is locked, a continuous service fills the gap.
Prerequisites
Termux:Boot add‑on.All steps are performed directly on the device; no PC is required, although you can use adb pull to retrieve logs later.
$ prompt.pkg update && pkg upgrade -y
logcat wrapper (part of the util-linux package) and busybox for extra utilities:
pkg install util-linux busybox -y
When you first launch Termux after a reboot, it may ask for permission to access storage. Accept the prompt, then run termux-setup-storage to create the /storage/emulated/0/Android/data/com.termux/files directory.
mkdir -p ~/logcat_logs
system_log, to avoid confusion with other files:
mv ~/logcat_logs ~/system_log
chmod 700 ~/system_log
These steps keep your logs private and avoid cluttering the shared Downloads folder.
The script will start logcat, pipe its output to a rotating file, and run in the background. Open the built‑in editor nano (installed by default) and create start_logcat.sh:
nano ~/start_logcat.sh
#!/data/data/com.termux/files/usr/bin/bash
# Directory where logs are stored
LOG_DIR="$HOME/system_log"
# Maximum size of a single log file (e.g., 5 MB). Adjust as needed.
MAX_SIZE=5242880
# Number of rotated logs to keep
MAX_FILES=7
# Current timestamp for file naming
TS=$(date +"%Y%m%d_%H%M%S")
# Full path of the active log file
ACTIVE_LOG="$LOG_DIR/logcat_$TS.txt"
# Start logcat in the background, redirecting both stdout and stderr
logcat -v threadtime *:V > "$ACTIVE_LOG" &
LOG_PID=$!
# Function to rotate logs when size limit is reached
rotate_logs() {
while kill -0 $LOG_PID 2>/dev/null; do
CUR_SIZE=$(stat -c%s "$ACTIVE_LOG")
if [ $CUR_SIZE -ge $MAX_SIZE ]; then
# Close current logcat process
kill $LOG_PID
# Start a new log file
TS=$(date +"%Y%m%d_%H%M%S")
ACTIVE_LOG="$LOG_DIR/logcat_$TS.txt"
logcat -v threadtime *:V > "$ACTIVE_LOG" &
LOG_PID=$!
fi
sleep 30
done
}
# Run the rotation monitor in the background
rotate_logs &
chmod +x ~/start_logcat.sh
This script does three things:
logcat with the -v threadtime format, which includes timestamps, PID, and thread ID – the most useful layout for debugging.~/system_log.Termux:Boot runs any executable placed in ~/.termux/boot after the Android boot sequence completes. Follow these steps:
mkdir -p ~/.termux/boot
cp ~/start_logcat.sh ~/.termux/boot/
chmod +x ~/.termux/boot/start_logcat.sh
ps | grep logcat. You should see a logcat process running.ls -lh ~/system_log. You should see at least one .txt file.If the script does not start, verify that Termux:Boot has permission to run in the background (Settings > Apps > Termux:Boot > Battery → “Unrestricted” or “Allow background activity”).
There are three convenient ways to get the logs off the device:
Android/data/com.termux/files/home/system_log and copy the .txt files.adb installed):
adb pull /data/data/com.termux/files/home/system_log ./android_logs
This command pulls the entire log directory to a local folder named android_logs.termux-share and run:
termux-share -a send -c text/plain -f ~/system_log/logcat_*.txt
This opens Android’s share sheet, allowing you to email, upload to cloud storage, or send via messaging.Once you have the files, you can open them in any text editor or import them into log‑analysis tools such as Logcat Viewer or grep for keyword searches.
If the device handles private data (e.g., corporate devices), you may want to encrypt the log files before they leave the phone. Termux provides openssl out of the box:
pkg install openssl -y
mykey with a strong passphrase):
openssl aes-256-cbc -salt -in ~/system_log/logcat_20230101_120000.txt -out ~/system_log/logcat_20230101_120000.enc -k mykey
shred -u ~/system_log/logcat_20230101_120000.txt
openssl aes-256-cbc -d -in ~/system_log/logcat_20230101_120000.enc -out ~/system_log/logcat_20230101_120000.txt -k mykey
For routine encryption, consider wrapping the above commands into a small script that runs after each rotation.
On Android 10+ the READ_LOGS permission is restricted to system apps. However, when you enable Developer options → Enable USB debugging, the logcat command works for the current user. If you still see the error:
If you notice storage filling up despite the rotation logic, try these adjustments:
MAX_SIZE=2097152 for 2 MB).*:V with *:I to capture only informational messages and above.-s filters, e.g., logcat -v threadtime -s ActivityManager:I SystemServer:I *.Common causes:
chmod +x ~/.termux/boot/start_logcat.sh.~/.termux/boot/. A stray sub‑folder will prevent execution.On newer Android versions, the /data/data/com.termux directory is not accessible without root. Use the public storage path instead:
adb pull /sdcard/Android/data/com.termux/files/home/system_log ./android_logs
This works because Termux creates a symlink from its private home to the shared /sdcard location when you run termux-setup-storage.
Once the service is active, you will see a logcat process listed in ps. New .txt files will appear in ~/system_log roughly every time the size limit is reached (default 5 MB). The rotation mechanism ensures you always have the most recent week of logs, and older files are automatically deleted.
The basic framework can be adapted for other monitoring needs:
logcat with tcpdump (available via pkg install tcpdump) and store PCAP files.dumpsys batterystats in a similar rotating script.adb bugreport output to the same directory on demand.Because everything runs inside Termux, you can combine multiple scripts, schedule them with cron (via the cronie package), or even push logs to a remote server using scp or rsync for centralized analysis.
With this guide you now have a reliable, non‑root method to capture, retain, and analyse Android system logs on any device. Whether you’re a developer hunting a bug, a power user monitoring battery health, or an IT admin needing audit trails, the Termux‑based log collector gives you the visibility you need without compromising device integrity.









