Skip to content

Instantly share code, notes, and snippets.

@suwardhana
Created February 15, 2026 04:42
Show Gist options
  • Select an option

  • Save suwardhana/cf5d362437395d748b44cc144190d184 to your computer and use it in GitHub Desktop.

Select an option

Save suwardhana/cf5d362437395d748b44cc144190d184 to your computer and use it in GitHub Desktop.
auto launch scrcpy on android device connect
#!/bin/bash
export PATH="/Users/suwardhana/Library/Android/sdk/platform-tools:$PATH"
ADB="/Users/suwardhana/Library/Android/sdk/platform-tools/adb"
SCRCPY="/usr/local/bin/scrcpy"
LOG="/tmp/monitor-android.log"
echo "Starting Android device monitor..." >> "$LOG"
while true; do
DEVICE_COUNT=$($ADB devices | awk '/device$/{count++} END{print count+0}')
SCRCPY_RUNNING=$(pgrep -x scrcpy >/dev/null && echo true || echo false)
if [ "$DEVICE_COUNT" -gt 0 ] && [ "$SCRCPY_RUNNING" = false ]; then
echo "Device connected. Launching scrcpy..." >> "$LOG"
"$SCRCPY" >/dev/null 2>&1 &
elif [ "$DEVICE_COUNT" -eq 0 ] && [ "$SCRCPY_RUNNING" = true ]; then
echo "Device disconnected. Stopping scrcpy..." >> "$LOG"
pkill -x scrcpy
fi
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment