Skip to content

Instantly share code, notes, and snippets.

@mrbar42
Last active May 22, 2024 19:06
Show Gist options
  • Save mrbar42/6f3cdc1be840ecbcc88694e450ebfc5c to your computer and use it in GitHub Desktop.
Save mrbar42/6f3cdc1be840ecbcc88694e450ebfc5c to your computer and use it in GitHub Desktop.
Endless logcat on device (no terminal) with log rotation and convenience commands.
#!/usr/bin/env bash
set -e
case $1 in
help)
cat << EOF
Capture logcat endlessly inside the device.
This should be ran on a computer that is connected to an android device using adb.
Commands:
status - show capture and stored logs status
start - start capturing logs. previous instance will be stopped
stop - stop capturing logs
pull - copy all capture log files to this computer
clean - remove all captured log files from the device
help - this message
EOF
;;
clean)
echo "Cleaning all existing log files in /sdcard/logcat/*"
adb shell rm -fr /sdcard/logcat/
;;
start)
echo "Starting log capture in the background in /sdcard/logcat/logcat.log*
use the status command for more info:
$0 status"
adb shell <<EOF
kill -9 \$(cat /sdcard/logcat-capture.pid 2>/dev/null) 2>/dev/null
mkdir -p /sdcard/logcat
nohup logcat -f /sdcard/logcat/logcat.log -r 20000 -n 10 '*:D' >/dev/null 2>&1 &
echo "\${!}" > /sdcard/logcat-capture.pid
EOF
;;
stop)
adb shell <<EOF
if [ "\$(cat /sdcard/logcat-capture.pid 2>/dev/null || echo '')" ]; then
echo "Found running capture - stopping"
kill "\$(cat /sdcard/logcat-capture.pid 2>/dev/null || echo '')"
else
echo "No running capture was found"
fi
rm -fr /sdcard/logcat-capture.pid
EOF
;;
pull)
echo "Pulling log files to current dir /sdcard/logcat/ -> ./logcat"
adb pull "/sdcard/logcat/"
;;
status)
adb shell <<EOF
if [ "\$(kill -0 "\$(cat /sdcard/logcat-capture.pid 2>/dev/null)" 2>/dev/null && echo "1" || echo "")" ]; then
echo "Job is running pid:\$(cat /sdcard/logcat-capture.pid)"
else
echo "No running job was found"
fi
echo "List of stored log files in '/sdcard/logcat/*':"
ls -lh /sdcard/logcat/* | grep 'logcat.log' | awk '{ print " "\$6" "\$7" "\$5" "\$8 }'
EOF
;;
esac
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment