JeffLIrion / adb_shell

A Python implementation of ADB with shell and FileSync functionality.
Apache License 2.0
530 stars 60 forks source link

Background a task #147

Closed relaxedricky closed 2 years ago

relaxedricky commented 3 years ago

Is their a way to run a task in the following way:

device.shell('su -c "nohup ./bla &"')

and have it go into the background without adb_shell timing out with:

adb_shell.exceptions.TcpTimeoutException: Reading from <IO>:5555 timed out (10.0 seconds)

Thanks

atti92 commented 3 years ago

I'm having some problems with setting timeouts too, it always goes back to 10.0 seconds. Even if I set every possible timeout higher on connect and init. With a high timeout I could probably do this on a thread.

JeffLIrion commented 3 years ago

Which version of the package are you using?

Using the latest version, it should be possible to set a very large timeout.

https://github.com/JeffLIrion/adb_shell/blob/622790cc81dd26bd529d77d56d93f8985260a4cb/adb_shell/adb_device.py#L419

But note that the timeouts follow a non-decreasing order:

https://github.com/JeffLIrion/adb_shell/blob/622790cc81dd26bd529d77d56d93f8985260a4cb/adb_shell/hidden_helpers.py#L89-L98

relaxedricky commented 3 years ago

I am using version 0.3.0 it seems:

(droid-Wcv5tG3s) user@coldpi:~/droid$ pip show adb_shell
Name: adb-shell
Version: 0.3.0

I want to be able to run a command in the background though, this is an instance where the app should be run and left running. I don't need to see the output from the app etc, it is a server that I will then use another client to connect to.

JeffLIrion commented 3 years ago

I think @atti92 is on the right track with running this shell command in a separate thread. The one thing that might not work about that approach is that, as I recall, all shell commands that this package sends use the same local ID. So I think you could send a shell command with a really long timeout, but I don't think you could send any more shell commands using this package.

jvmk commented 3 years ago

I think @atti92 is on the right track with running this shell command in a separate thread. The one thing that might not work about that approach is that, as I recall, all shell commands that this package sends use the same local ID. So I think you could send a shell command with a really long timeout, but I don't think you could send any more shell commands using this package.

I can confirm that if you invoke a long-running blocking command on thread t1 and then attempt to invoke other commands simultaneously on thread t2, you will get BlockingIOError: [Errno 11] Resource temporarily unavailable errors. The (short) calls on t2 actually seem to make it through to the device, but it's hard to say what is lost/corrupted so I'd stay away from this strategy.

I thought I'd be able to usenohup and & to run the command in the background. I'd like to record the screen while sending other commands, so I tried:

tcpdevice = ... # initialize and connect an AdbDeviceTcp
res = tcpdevice.shell('nohup screenrecord --time-limit 30 /sdcard/myrecording.mp4 &')

This returns immediately (doesn't block), but the device doesn't start recording (/sdcard/myrecording.mp4 never gets created and there's no trace of any screenrecord in ps output). res seems to just be an empty string. Any idea why this approach doesn't work with adb_shell?

Other potential hacky alternatives:

Any other ideas would be greatly appreciated!

Thanks, Janus

JeffLIrion commented 3 years ago

Backgrounding a task should be possible with this pull request: https://github.com/JeffLIrion/adb_shell/pull/172

You can install that branch via

pip install https://github.com/JeffLIrion/adb_shell/archive/support-multiple-streams.zip

I think you have two options:

  1. Run a shell command with a large timeout in a separate thread.
  2. Run a streaming shell command and call next on the generator as needed. This should be more efficient.
JeffLIrion commented 3 years ago

This should be possible using the latest release (0.4.0), as described in my previous comment.

pip install -U adb-shell
barkside commented 11 months ago

Hi @JeffLIrion Is there an example anywhere, or doc, on how to run 2 adb shell commands simultaneously? I'm struggling to find anything, or get anything working. Thanks!