Skip to content

Instantly share code, notes, and snippets.

@kairusds
Last active September 11, 2025 19:09
Show Gist options
  • Save kairusds/1d4e32d3cf0d6ca44dc126c1a383a48d to your computer and use it in GitHub Desktop.
Save kairusds/1d4e32d3cf0d6ca44dc126c1a383a48d to your computer and use it in GitHub Desktop.
Instructions for connecting Termux's android-tools adb to the current device via Wireless debugging and fixing phantom process killing

Install android-tools if you haven't already:

pkg update ; pkg upgrade
pkg install android-tools

adb pair 127.0.0.1:port

Where port is taken from the menu shown after clicking from Developer options > Wireless debugging > Pair device with pairing code. Use splitscreen to show the Wireless debugging setting below the Termux app when pairing.


After pairing successfully, run the following commands to either connect or disconnect:

adb connect 127.0.0.1:port

adb disconnect

Where port is shown in the Wireless debugging menu as IP address & Port.


After you're done with adb, make sure to close the daemon:

adb kill-server

Notice: Disabling phantom process killing might cause battery drainage issues according to this comment, disable it at your own risk. From my experience, I never experienced this issue but that might be because I'm on MIUI so it might be more problematic if your phone OEM doesn't have an aggresive battery saver feature like Samsung or Xiaomi does.

After Termux has connected via Wireless debugging, run the command below to fix phantom process killing. (Android 12L and above)

adb shell "settings put global settings_enable_monitor_phantom_procs false"
@RainbowC0
Copy link

In Termux, all IP address can be set as 127.0.0.1. You can only focus on the port and pairing code information.

@kairusds
Copy link
Author

In Termux, all IP address can be set as 127.0.0.1. You can only focus on the port and pairing code information.

Oh ty for the tip, I wasn't aware of that.

@weskerty
Copy link

weskerty commented Oct 3, 2024

Thank you very much. Just what I needed.

@weskerty
Copy link

adb shell "/system/bin/device_config set_sync_disabled_for_tests persistent"

adb shell "/system/bin/device_config put activity_manager max_phantom_processes 2147483647"

adb shell settings put global settings_enable_monitor_phantom_procs false

adb shell cmd deviceidle whitelist +com.termux

adb shell cmd appops set com.termux RUN_IN_BACKGROUND allow

adb shell cmd appops set com.termux RUN_ANY_IN_BACKGROUND allow

adb shell cmd appops set com.termux SYSTEM_EXEMPT_FROM_ACTIVITY_BG_START_RESTRICTION allow

adb shell cmd appops set com.termux SYSTEM_EXEMPT_FROM_HIBERNATION allow

adb shell cmd appops set com.termux SYSTEM_EXEMPT_FROM_POWER_RESTRICTIONS allow

adb shell cmd appops set com.termux SYSTEM_EXEMPT_FROM_SUSPENSION allow

adb shell cmd appops set com.termux WAKE_LOCK allow

@Rudxain
Copy link

Rudxain commented Jan 20, 2025

I hate that the pairing-port and connection-port are always different. It caused me so many headaches when I tried to do the setup without tutorials. Is this Google's fault or a technical limitation of TCP/IP?

@weskerty
Copy link

I hate that the pairing-port and connection-port are always different.

Google thing
A question of "security"

@Rudxain
Copy link

Rudxain commented Jan 21, 2025

A question of "security"

lol. I guess that's fair

@AlexanderMcColl
Copy link

Just wanted to say thank you so much for this guide, it made setting up Wavelet a breeze.

@kairusds
Copy link
Author

Just wanted to say thank you so much for this guide, it made setting up Wavelet a breeze.

you're welcome :)

@navid-zamani
Copy link

I hate that the pairing-port and connection-port are always different. It caused me so many headaches when I tried to do the setup without tutorials. Is this Google's fault or a technical limitation of TCP/IP?

This is absolutely Google’s fault.
I just run adb tcpip $myFavoritePort after boot, and it stays on that port until reboot.

With Termux, I can even run that command right in Termux and don’t have to connect via USB nor enter the port manually at all, to set it back to $myFavoritePort. (You need to pair to 127.0.0.1:$googlesRandomPort via entering a pairing code before doing it the first time.)

@navid-zamani
Copy link

BEWARE

BEWARE before disabling these process limitations!
It drained my battery badly, whenever some process kept running in the background.

BEWARE even more that Android is intentionally lying to you about which processes run!
E.g. in Termux and when using adb shell run-as com.termux, I see two different results! Even though it is the same user! And in just adb shell, I see other results again!
I even got into situations where clearly sshd was running (cause I’m freaking using it!), but it just tells me no process matching “sshd” is running at all, and I cannot kill it!
I had to reboot, to get my battery to not drain anymore!

I suspect some severe SElinux and namespace shenenigans.

@weskerty
Copy link

This behavior is reported in the Termux tutorials. Even if you close Termux, some processes may not die and may remain running.

@kairusds
Copy link
Author

kairusds commented Sep 11, 2025

Interesting, I guess this is one of the few moments where I'm actually thankful for some Android OEM's aggressive battery saver features. Because I never noticed this becoming a problem once I use "Force stop" on the app in MIUI. I'll add this notice to the original post just in case

BEWARE

BEWARE before disabling these process limitations! It drained my battery badly, whenever some process kept running in the background.

BEWARE even more that Android is intentionally lying to you about which processes run! E.g. in Termux and when using adb shell run-as com.termux, I see two different results! Even though it is the same user! And in just adb shell, I see other results again! I even got into situations where clearly sshd was running (cause I’m freaking using it!), but it just tells me no process matching “sshd” is running at all, and I cannot kill it! I had to reboot, to get my battery to not drain anymore!

I suspect some severe SElinux and namespace shenenigans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment