5

I know how to open the termux app, run (for example) sv up sshd, and then interact with the Android device via ssh, rsync, etc. This is just an example, and the same is also true for any other termux initiated procedures.

However, to do this, it's necessary to perform that manual open and manual sv up sshd command in termux on the device itself.

I want to be able to perform that initial termux interaction from my desktop computer via adb without any direct interaction with the device itself, aside from plugging in the USB cable.

I have looked for solutions, but it seems like no one has come up with any way to interact with termux except by first actually manually opening that app on the device, which I do not want to do.

However, could I have overlooked something? I'm hoping that I indeed missed something, and that somehow I can start termux-initiated procedures via adb.

Does anyone know how that might be done?

Thank you in advance.

2
  • 2
    ADB shell is just an app (with UID 2000). And apps cannot access other apps' directories without root access. Related: How to run a program in an app context with Magisk?. // You can add sshd to .bashrc or .profile (so that it's started whenever Termux is launched) and then launch the app using adb shell am start -n com.termux/.HomeActivity. May 22, 2020 at 2:42
  • I have root, but even so, I see from the related article that I would have to run a complicated procedure to get the described functionality. However, using .profile and the am start command are doing what I want, so that's good enough for me, for now. Some day I will try to encapsulate that other multi-step procedure into some sort of utility, however, because programmatically running termux-based commands would be quite useful. Thank you!
    – HippoMan
    May 22, 2020 at 12:24

4 Answers 4

3

You can use run-as to access your Termux environment from adb without root or ssh.

Run adb shell and then:

run-as com.termux files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; bash -i'

This will give you a Termux shell.

4
  • Aha! This is what I've been looking for and hoping for during all this time. I want an interactive termux shell that I can use from my desktop computer, and which provides this exact functionality. Thank you!
    – HippoMan
    Nov 2, 2023 at 0:12
  • ... and I can even do the following: (1) put that command into a file called /sdcard/termux-shell.sh; (2) run the following from my desktop computer: adb shell exec /system/bin/sh /sdcard/termux-shell.sh
    – HippoMan
    Nov 2, 2023 at 0:18
  • This fails for me with the error: run-as: package not debuggable: com.termux My understanding from this message that Termux is not set as debuggable and would require recompiling with a flag set to true which isn't possible for me.
    – Michael
    Nov 12, 2023 at 15:22
  • @Michael: You may need the latest APK release, v0.118.0. The version on the Play store has long been deprecated and should not be used. Unclear whether the version distributed on F-Droid is debuggable, check by running echo $TERMUX_IS_DEBUGGABLE_BUILD, which will be 1 if your version of Termux is a debuggable build.
    – josh3736
    Nov 13, 2023 at 3:26
4

Per the comment by Irfan Latif above, the following works:

  1. Launch termux

  2. Do the following:

    echo 'sv up sshd
    sshd started' > .profile
    
  3. Run this whenever I want to start sshd via adb:

    adb shell am start -n com.termux/.HomeActivity
    
1
  • Thank you! This suggestion works, but it configures termux to always start up sshd, even if I am running it interactively via the Android device. I want to be able to run termux interactively both via the Android device and via an external adb connection.
    – HippoMan
    Nov 2, 2023 at 0:35
4
adb shell am start -n com.termux/.HomeActivity
adb shell input text "your command here"
adb shell input keyevent ENTER
2
  • Yes, that works, but it only runs one command at a time. I've been looing for a way to remotely run an interactive termux shell, where I type in commands and they each get executed one by one, just like what happens after I invoke "adb shell". The answer by @josh3736 accomplishes this.
    – HippoMan
    Nov 2, 2023 at 0:33
  • this doesn't seem to work if the screen is off on the device
    – Michael
    Nov 12, 2023 at 15:27
0

2024 proper solution

Termux allows you to start the run command service through am allowing you to pass extras as well

adb shell /system/bin/am startservice --user 0 -n com.termux/com.termux.app.RunCommandService \
-a com.termux.RUN_COMMAND \
--es com.termux.RUN_COMMAND_PATH '/data/data/com.termux/files/usr/bin/sleep' \
--esa com.termux.RUN_COMMAND_ARGUMENTS '10' \
--es com.termux.RUN_COMMAND_WORKDIR '/data/data/com.termux/files/home' \
--ez com.termux.RUN_COMMAND_BACKGROUND 'true' \
--es com.termux.RUN_COMMAND_SESSION_ACTION '0'

In this command notice how I set the background option to true which is important so that it automatically runs in the background(the notification will show you that a task is being run in the background)

In the example above, we are executing the sleep command and passing in 10 as an argument which will make termux basically execute sleep 10 Once the execution is completed, termux will automatically exit

More information can be found here

https://github.com/termux/termux-app/wiki/RUN_COMMAND-Intent#basic-examples

[Message to mod who deleted my previous post(how do I contact you?)]

1
  • This didn't work for me. I got: Starting service: Intent { act=com.termux.RUN_COMMAND cmp=com.termux/.app.RunCommandService (has extras) }. Error: Requires permission com.termux.permission.RUN_COMMAND I guess this works only on a rooted phone (mine isn't)
    – Scooby-2
    Feb 5 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .